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



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

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

默认长度为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)

相关内容

  • 没有找到相关文章

最新更新