簡體   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