简体   繁体   English

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

[英]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.默认长度为 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()

You need to declare the variable global in the function that assigns it, and convert to integer.您需要在分配它的函数中声明变量 global,并将其转换为整数。

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

暂无
暂无

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

相关问题 为什么在QPlainTextEdit中输入时,光标的长度会改变? - Why does the length of the cursor change when I input in QPlainTextEdit? 我不知道为什么字符串的长度为&#39;0&#39; - I don't know why the length of string is '0' 为什么我的长度为 1 的字符串变为长度为 3? - Why does my string of length 1 change to length 3? Python - 如果我增加列表的大小,为什么我的 for 循环的长度 function 不会改变 - Python - Why doesn't the length function change for my for loop if I'm increasing the size of a list 为什么即使我在循环中更改长度,Python 的“for”也会迭代列表的长度? - Why does Python's "for" iterate the length of the list even if I change the length in the loop? 使用 random.sample() 时,为什么我得到的值长度 (1) 与索引 (3) 的长度不匹配? - Why do I get Length of values (1) does not match length of index (3) when using random.sample()? 当我 append 我的空列表时,为什么我的列表长度为 1? - Why is my list of length 1 when i append my empty list? while循环不会中断,当计数器不小于数组长度时应中断 - while loop won't break,should break when the counter is no longer less than the length of the array 密码没有创建到指定的长度? - Password is not created to specified length? 当我将鼠标悬停在pygame上时,为什么我的按钮不会更改颜色? - Why won't my button change color when i hover over it pygame?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM