我正在尝试构建一个创建密码的gui,并且我已经生成了密码并使其显示在标签中。但是,当多次单击该按钮时,似乎旧密码不会消失,它只是覆盖在顶部。我也得到一个我似乎无法纠正的错误,虽然它似乎不会影响gui。
目前的代码是:
from tkinter import *
import random
myGui = Tk()
myGui.geometry('300x200+700+250')
myGui.title('Password Generator')
def passwordgen():
password = ''
for i in range(8):
##----runs the for loop 8 times
if (i == 0) or (i == 4):
password = password + chr(random.randint(97, 122))
if (i == 1) or (i == 5):
password = password + chr(random.randint(65, 90))
if (i == 2) or (i == 6):
password = password + chr(random.randint(48, 57))
if (i == 3) or (i == 7):
password = password + chr(random.randint(33, 47))
passLabel = Label(myGui, text=password)
passLabel.grid(row=0, column=1, sticky=E)
genPassBtn = Button(myGui, text="Generate Password", command=passwordgen)
genPassBtn.bind("<Button-1>", passwordgen)
genPassBtn.grid(row=0, column=0, sticky=W)
myGui.mainloop()
我收到的错误是:
return self.func(*args)
TypeError: passwordgen() takes 0 positional arguments but 1 was given
我希望实现的结果是创建一个生成密码的gui,为生成的密码生成哈希值,检查密码强度,将生成的哈希加载到文本文件,然后可以针对存储的哈希验证密码
此外,根据收到的建议,我已经修改了代码,并增加了额外的检查力度。代码现在看起来像这样:
from tkinter import *
import random
myGui = Tk()
myGui.geometry('300x200+700+250')
myGui.title('Password Generator')
def passwordgen():
password = ''
for i in range(8):
##----runs the for loop 8 times
if (i == 0) or (i == 4):
password = password + chr(random.randint(97, 122))
if (i == 1) or (i == 5):
password = password + chr(random.randint(65, 90))
if (i == 2) or (i == 6):
password = password + chr(random.randint(48, 57))
if (i == 3) or (i == 7):
password = password + chr(random.randint(33, 47))
strPassword.set(password)
def checkPassword():
strength = ['Blank', 'Very Weak', 'Weak', 'Medium', 'Strong', 'Very Strong']
score = 1
password = strPassword.get()
if len(password) < 1:
return strength[0]
if len(password) < 4:
return strength[1]
if len(password) >= 8:
score += 1
if re.search('[0-9]', password):
score += 1
if re.search('[a-z]', password) and re.search('[A-Z]', password):
score += 1
if re.search('.', password):
score += 1
passwordStrength.set(strength[score])
genPassBtn = Button(myGui, text="Generate Password", command=passwordgen)
strPassword = StringVar()
lblPassword = Label(myGui, textvariable=strPassword)
lblPassword.grid(row=0, column=1, sticky=W)
genPassBtn.grid(row=0, column=0, sticky=W)
passwordStrength = StringVar()
checkStrBtn = Button(myGui, text="Check Strength", command=checkPassword)
checkStrBtn.grid(row=1, column=0)
checkStrLab = Label(myGui, textvariable=passwordStrength)
checkStrLab.grid(row=1, column=1)
myGui.mainloop()
答案 0 :(得分:3)
试试这个例子。
from tkinter import *
import random
myGui = Tk()
myGui.geometry('300x200+700+250')
myGui.title('Password Generator')
def passwordgen():
password = ''
for i in range(8):
##----runs the for loop 8 times
if (i == 0) or (i == 4):
password = password + chr(random.randint(97, 122))
if (i == 1) or (i == 5):
password = password + chr(random.randint(65, 90))
if (i == 2) or (i == 6):
password = password + chr(random.randint(48, 57))
if (i == 3) or (i == 7):
password = password + chr(random.randint(33, 47))
strPassword.set(password)
genPassBtn = Button(myGui, text="Generate Password", command=passwordgen)
strPassword = StringVar()
lblPassword = Label(myGui, textvariable=strPassword)
lblPassword.grid(row=0,column=1, sticky=W)
genPassBtn.grid(row=0, column=0, sticky=W)
myGui.mainloop()
这就是我所做的