pythontkinterpassword-generator

My Password generator on tkinter is overusing the if/elif and i'm looking for alternatives


Im new to python and tkinter and was wondering a better alternative to the if/elif statements. This is not the most efficient code but I just wanted to know what can work with .get() when the conditions are constantly different

def generate(password):
    display_box.delete(0, END)
    password = ""
    password_length = password_scale.get()
    password_count = 0
    while password_count < password_length:
        #If all options are selected
        if (lower_var.get() == 1) & upper_var.get() == 1 & symbols_var.get() == 1:
            password += random.choice(
                uppercase_option + lowercase_option + symbol_option
            )
            password_count += 1
        #Upper and lower
        elif (lower_var.get() == 1) & (upper_var.get()):
            password += random.choice(uppercase_option + lowercase_option)
            password_count += 1
        #Lower and symbols
        elif (lower_var.get() == 1) & (symbols_var.get() == 1):
            password += random.choice(lowercase_option + symbol_option)
            password_count += 1
        #upper and symbols
        elif (upper_var.get() == 1) & (symbols_var.get() == 1):
            password += random.choice(uppercase_option + symbol_option)
            password_count += 1
        #Lower only
        elif lower_var.get() == 1:
            password += random.choice(lowercase_option)
            password_count += 1
        #Upper only
        elif upper_var.get() == 1:
            password += random.choice(uppercase_option)
            password_count += 1
        #symbols only
        elif symbols_var.get() == 1:
            password += random.choice(symbol_option)
            password_count += 1
        else:
            password_count += 1

    display_box.insert(0, password)

Solution

  • You just need to construct the character list once based on the selected categories and generate the password using the constructed character list:

    def generate():
        sources = [lowercase_option, uppercase_option, symbol_option]
        selections = [lower_var.get(), upper_var.get(), symbols_var.get()]
        # construct the character list based on selected categories
        choices = ''.join(x for x, selected in zip(sources, selections) if selected)
        # generate the password
        passwd = ''.join(random.choice(choices) for _ in range(password_scale.get()))
        # show the generated password
        display_box.delete(0, "end")
        display_box.insert("end", passwd)