简体   繁体   中英

Why won't the password length change when I put the desired length in the textbox?

When I submit the desired password length, the length doesn't change.

There are two buttons: the top one (which is smaller), is for submitting the password's length. The bottom one (bigger), generates the password, taking the length that was inputted.

The default length is 12 characters, though I will make the minimum length 8, and the maximum 16.

def copy():
    copy_pw = Tk()
    copy_pw.withdraw()
    copy_pw.clipboard_clear()
    copy_pw.clipboard_append(password)
    copy_pw.update()

def password_generator():
    lower_case = "abcdefghijklmnopqrstuvwxyz"
    upper_case = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    numbers = "0123456789"
    symbol = "%^#*:;._-@`~"

    answer = lower_case + upper_case + numbers + symbol

    global password_length
    password_length = 12

    global password
    password = "".join(random.sample(answer, password_length))
    print("Password has been generated: ", password)
    text.config(text = password)

def submit_length():
    user_length = entry.get()
    password_length = user_length

window = Tk()
window.title("Password Generator")

length = Button(window, text = 'Enter')
length.pack()
length.config(command = submit_length)
length.config(font =('Segoe UI', 10))
length.config(bg = '#009DFF')
length.config(fg = '#ffffff')
length.config(activebackground = '#009DFF')
length.config(activeforeground = '#ffffff')

entry = Entry()
entry.pack()
entry.config(font = ('Segoe UI', 12))

button = Button(window, text = 'Generate password')
button.pack()
button.config(command = password_generator)
button.config(font =('Segoe UI', 22))
button.config(bg = '#009DFF')
button.config(fg = '#ffffff')
button.config(activebackground = '#009DFF')
button.config(activeforeground = '#ffffff')

text = Label(window, text = password)
text.pack()
text.config(font = ('Monospace', 25))
button.pack()

# copy password
copy_password = Menu(text, tearoff= 0, bg = "white", fg = "black")
copy_password.add_command(label="Copy", command=copy)

# popup on right click
text.bind("<Button - 3>", popup)

window.mainloop()

You need to declare the variable global in the function that assigns it, and convert to integer.

def submit_length():
    global password_length
    user_length = entry.get()
    password_length = int(user_length)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM