[英]Password generator with tkinter
Iam going to write a program in which it generates a random password when I press the Generate button and shows it in a box.我将编写一个程序,当我按下“生成”按钮并将其显示在一个框中时,它会生成一个随机密码。 and below it a stopwatch starts to countdown.I have written it and I dont have problem in generating password.
在它下面有一个秒表开始倒计时。我已经写了它,我在生成密码时没有问题。 my only problem is my stopwatch works only once and after that whenever I press generate I only get random password and stopwatch does not work anymore.
我唯一的问题是我的秒表只工作一次,之后每当我按下生成时,我只会得到随机密码,秒表不再工作。 here is my code:
这是我的代码:
import random,time
from tkinter import *
win=Tk()
win.title('Password generator')
def PasswordMaker():
e1.delete(0,'end')
small_letters = list(map(chr, range(ord('a'), ord('z') + 1)))
big_letters = list(map(chr, range(ord('A'), ord('Z') + 1)))
digits = ""
i = random.randint(0, 25)
j = random.randint(0, 25)
k = random.randint(0, 1)
for _ in range(0, 6):
digits += str(random.randint(0, 9))
lst = list(big_letters[j] + small_letters[i] + digits)
random.shuffle(lst)
password="".join( lst[h] for h in range(8))
password=str(password)
e1.insert(END,password)
t = 3
def countdown():
global t
if t > 0:
print(t)
l1.config(text=t)
t = t - 1
l1.after(1000, countdown)
elif t == 0:
print('end')
lb=Label(win,text="Password Generator",font=('arial 20')).pack()
e1=Entry(win,font=('arial 13'))
e1.pack()
lb2=Label(win,text="Remaining Time :",font=('arial 10')).pack()
bt=Button(win,text="Generate",font=('arial 15'),fg='red',command=lambda :
[PasswordMaker(),countdown()])
bt.pack()
l1 = Label(win, font='arial 20')
l1.pack()
win.mainloop()
All you have to do is to reset back the t
to 3 everytime you hit that Generate
button.您所要做的就是在每次点击
Generate
按钮时将t
重置为 3。
Just change PasswordMaker
to:只需将
PasswordMaker
更改为:
def PasswordMaker():
global t # ----------------------> Here
e1.delete(0, 'end')
small_letters = list(map(chr, range(ord('a'), ord('z') + 1)))
big_letters = list(map(chr, range(ord('A'), ord('Z') + 1)))
digits = ""
i = random.randint(0, 25)
j = random.randint(0, 25)
k = random.randint(0, 1)
for _ in range(0, 6):
digits += str(random.randint(0, 9))
lst = list(big_letters[j] + small_letters[i] + digits)
random.shuffle(lst)
password = "".join(lst[h] for h in range(8))
password = str(password)
e1.insert(END, password)
t = 3 # -----------------------> Here
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.