简体   繁体   English

Python Tkinter标签刷新

[英]Python Tkinter label refresh

I'm trying to build a gui that creates a password and i've got as far as generating the password and making it appear in a label. 我正在尝试构建一个可创建密码的gui,并且我已经尽力生成密码并将其显示在标签中。 However when the button is clicked multiple times it appears the old password doesnt dissapear, it just overlays on top. 但是,当多次单击该按钮时,似乎不会消失旧密码,它只是覆盖在顶部。 I'm also getting an error that i cant seem to rectify, although it doesnt seem to affect the gui. 我也遇到了我似乎无法纠正的错误,尽管它似乎并未影响gui。

The code so far is: 到目前为止的代码是:

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()

The error i receive is: 我收到的错误是:

return self.func(*args)
TypeError: passwordgen() takes 0 positional arguments but 1 was given

The outcome i am hoping to achieve is to create a gui that generates a password, generates a hash value for generated password, checks the password strength, loads the generated hash to a text file and then can verify the password against stored hashes. 我希望实现的结果是创建一个gui,该gui可以生成密码,为生成的密码生成哈希值,检查密码强度,将生成的哈希值加载到文本文件中,然后可以针对存储的哈希值验证密码。

Further on now and from advice received i have amended the code and added extra to check the strength. 现在,根据收到的建议,我修改了代码,并添加了更多内容以检查强度。 The code now looks like this: 现在的代码如下所示:

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()

Try this example. 试试这个例子。

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()

Here's what I've done 这就是我所做的

  1. Rather than creating a new label each time, I change the text of a label using the StringVar called strPassword. 我不是每次都创建一个新标签,而是使用称为strPassword的StringVar更改标签的文本。
  2. You don't need to bind a button to a click to call a function, using Button(... , command=myFunction) does this already. 您不需要将按钮绑定到单击即可调用函数,使用Button(...,command = myFunction)可以完成此操作。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM