gpt4 book ai didi

python - 如何避免 AttributeError : '_tkinter.tkapp' object has no attribute 'PassCheck'

转载 作者:太空狗 更新时间:2023-10-30 01:27:36 33 4
gpt4 key购买 nike

我已阅读过有关此错误的先前帖子,但无法确定我做错了什么。请有人帮助我了解我做错了什么,谢谢。

   from tkinter import *
class Passwordchecker():
def __init__(self):
self= Tk()
self.geometry("200x200")
self.title("Password checker")
self.entry=Entry(self)
self.entry.pack()
self.button=Button(self,text="Enter",command= lambda: self.PassCheck(self.entry,self.label))
self.button.pack()
self.label=Label(self,text="Please a password")
self.label.pack()
self.mainloop()
def PassCheck(self1,self2):
password = self1.get()
if len(password)>=9 and len(password)<=12:
self2.config(text="Password is correct")
else:
self2.config(text="Password is incorrect")

run = Passwordchecker()

最佳答案

是什么触发了错误?

您收到此错误消息:

AttributeError: '_tkinter.tkapp' object has no attribute 'PassCheck'

因为当 Passwordchecker() 的实例被初始化时,它偶然发现了 __init__()mainloop() 方法不要让您的程序识别属于该实例的任何其他方法。根据经验,永远不要__init__() 中运行 mainloop()。这完全修复了您在上面收到的错误消息。但是,我们还有其他问题需要解决,为此,让我们重新设计您的程序:

设计

最好求助于您在 __init__() 中调用的其他方法来绘制 GUI。我们称它为 initialize_user_interface()

当涉及到PassCheck()时,您首先需要将对象本身传递给该方法。这意味着传递给此方法的第一个参数是 self。这实际上是我们唯一需要的参数 PassCheck(self) 因为您可以从此方法访问您无用地传递给它的其余参数。

程序

所以这是您需要的完整程序:

import tkinter as tk
class Passwordchecker(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.parent = parent
self.initialize_user_interface()

def initialize_user_interface(self):
self.parent.geometry("200x200")
self.parent.title("Password checker")
self.entry=tk.Entry(self.parent)
self.entry.pack()
self.button=tk.Button(self.parent,text="Enter", command=self.PassCheck)
self.button.pack()
self.label=tk.Label(self.parent,text="Please a password")
self.label.pack()

def PassCheck(self):
password = self.entry.get()
if len(password)>=9 and len(password)<=12:
self.label.config(text="Password is correct")
else:
self.label.config(text="Password is incorrect")

if __name__ == '__main__':

root = tk.Tk()
run = Passwordchecker(root)
root.mainloop()

演示

这是运行程序的截图:

enter image description here

关于python - 如何避免 AttributeError : '_tkinter.tkapp' object has no attribute 'PassCheck' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38229857/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com