在tkinter中将变量调用到同一个类中的不同函数中

时间:2017-11-23 14:28:13

标签: python tkinter

我正在尝试在python中创建密码生成器和检查程序。目前我已经完成了gui,但是我无法更新tkinter标签并显示textvariable,因为它只是出现一个空格而不是'错误',弱'其他人帮助我使用了发电机,但不幸的是,他们所做的并不适用于这种情况。

class PageOne(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label1 = tk.Label(self, text="Check your password", font=controller.title_font)
        label1.pack(side="top", fill="x", pady=10)
        entry = tk.Entry(self, bd =6)
        button1 = tk.Button(self, text="Enter", command=checkPassword(entry.get()))
        self.update
        button2 = tk.Button(self, text="Back",
                   command=lambda: controller.show_frame("StartPage"))
        label2 = tk.Label(self, text="Strength:", font=controller.title_font)
        label3 = tk.Label(self, textvariable=passwordStrength, font=controller.title_font)
        entry.pack()
        button1.pack()
        button2.pack()
        label2.pack()
        label3.pack()

    def checkPassword(password):
        passwordStrength = tk.StringVar()
        strength = ['Password can not be Blank', 'Very Weak', 'Weak', 'Medium', 'Strong', 'Very Strong', 'Password must be less than 24 characters']
        score = 1
        print (password), len(password)

        if len(password) == 0:
            passwordStrength.set(strength[0])
            return

        if len(password) > 24:
            passwordStrength.set(strength[6])
            return

        if len(password) < 4:
            passwordStrength.set(strength[1])
            return

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

如果您需要有关我的项目或其他代码的更多信息,请询问。非常感谢所有帮助,提前感谢:)

1 个答案:

答案 0 :(得分:1)

&#34; 将变量调用到tkinter中同一类中的不同函数的答案&#34;就是将这些变量分配给类属性。

作为具有此类行为的tkinter示例,下面的代码会生成一个标签,并在1秒后调用一个函数来更改标签的文本,以查找类属性:

import tkinter as tk

class VariableAssignedAsAttribute(tk.Label):

    def __init__(self, master):
        super().__init__(master)

        aVariable = "some string"

        self.anAttribute = aVariable

        self['text'] = "Unchanged Text"

    def updateLabel(self):
        self.config(text=self.anAttribute)




root = tk.Tk()

lbl = VariableAssignedAsAttribute(root)
lbl.pack()

root.after(1000, lbl.updateLabel)

root.mainloop()

我会检查评论中提供的PM 2Ring引用。

相关问题