[英]GUI for a random password generator
I'm learning to use tkinter and ttk so I tried making a GUI for a random password generator and this is my code so far:我正在学习使用 tkinter 和 ttk,所以我尝试为随机密码生成器制作一个 GUI,这是我目前的代码:
import random
from tkinter import *
from tkinter import ttk
win = Tk()
win.geometry("300x250")
win.title('random pass generator')
global password_length
password_length = int()
enter_length = ttk.Entry(win , textvariable = password_length)
enter_length.pack()
global password
password = StringVar()
def generate_password(password_length):
i = 0
characters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
while i < password_length:
password = password + random.choice(characters)
i = i + 1
generate = ttk.Button(win,text = 'generate',command = generate_password(password_length))
generate.pack()
label = ttk.Label(win,textvariable = password)
label.config(background = 'black')
label.pack()
win.mainloop()
The problem is that it doesn't give me the password after I write the password's length and click the button.问题是在我输入密码长度并单击按钮后,它没有给我密码。
I tried to assign the password to a label to get it on screen but it doesn't show up after I click the button.我试图将密码分配给 label 以将其显示在屏幕上,但在我单击按钮后它没有显示。
As you have password
as a StringVar
, you need to use the set
method to assign it a value, eg,:由于您将password
作为StringVar
,您需要使用set
方法为其分配一个值,例如:
password.set(password.get() + random.choice(characters))
rather than:而不是:
password = password + random.choice(characters)
Also, the command
argument to Button
can only take a function handle rather than a call to a function, ie, you could only pass command=generate_password
and not command=generate_password(password_length)
.此外, Button
的command
参数只能采用 function 句柄而不是对 function 的调用,即,您只能传递command=generate_password
而不是command=generate_password(password_length)
。 You can either make password_length
global rather than being an argument to the function, hardcode it into the function, or use a lambda function (see here ), like:您可以将password_length
设为全局而不是作为 function 的参数,将其硬编码到 function 中,或者使用 lambda function(参见此处),例如:
generate = ttk.Button(
win,
text='generate',
command=lambda: generate_password(password_length)
)
Finally, if using the lambda
option above, you should also change:最后,如果使用上面的lambda
选项,您还应该更改:
password_length = int()
enter_length = ttk.Entry(win , textvariable = password_length)
to到
password_length = StringVar()
enter_length = ttk.Entry(win, textvariable=password_length)
as textvariable
should take a StringVar
rather than an int
.因为textvariable
应该采用StringVar
而不是int
。 And then within generate_password
, have:然后在generate_password
中,有:
while i < int(password_length.get()):
So, overall the code will be:因此,总体代码将是:
import random
from tkinter import *
from tkinter import ttk
win = Tk()
win.geometry("300x250")
win.title('random pass generator')
password_length = StringVar()
enter_length = ttk.Entry(win, textvariable=password_length)
enter_length.pack()
password = StringVar()
def generate_password(password_length):
i = 0
characters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
while i < int(password_length.get()):
password.set(password.get() + random.choice(characters))
i = i + 1
generate = ttk.Button(
win,
text='generate',
command=lambda: generate_password(password_length)
)
generate.pack()
label = ttk.Label(win,textvariable = password)
label.config(background='black')
label.pack()
win.mainloop()
Noting that, by setting the label
background to black you won't be able see the password text as it will also be black.请注意,通过将label
背景设置为黑色,您将看不到密码文本,因为它也是黑色的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.