繁体   English   中英

当我在文本框中输入所需的长度时,为什么密码长度不会改变?

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

当我提交所需的密码长度时,长度不会改变。

有两个按钮:顶部的一个(较小)用于提交密码的长度。 底部的(较大的)生成密码,取输入的长度。

默认长度为 12 个字符,但我将最小长度设置为 8,最大长度为 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()

您需要在分配它的函数中声明变量 global,并将其转换为整数。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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