Python 如何避免 AttributeError: '_tkinter.tkapp' 对象没有属性 'PassCheck'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38229857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 20:32:08  来源:igfitidea点击:

How to avoid AttributeError: '_tkinter.tkapp' object has no attribute 'PassCheck'

pythontkinter

提问by DrDevilHell

I have read previous posts regarding this error but could not identify what i was doing wrong.Please can someone help me understand what i am doing wrong,thank you.

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

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

回答by Billal Begueradj

What triggered the error?

是什么触发了错误?

You get this error message:

您收到此错误消息:

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

Because when an instance of Passwordchecker()is initialized, it stumbles on the mainloop()method of your __init__()which does not let your program to recognize any further method belonging to that instance. As a rule of thumb, NEVERrun mainloop()inside __init__(). This fixes fully the error message you got above. However, we have other things to fix, and for that, let us redesign your program:

因为当Passwordchecker()初始化的实例时,它会偶然mainloop()发现您的方法,该方法__init__()不允许您的程序识别属于该实例的任何其他方法。作为一个经验法则,从来没有运行mainloop()里面__init__()。这完全修复了您在上面收到的错误消息。但是,我们还有其他事情需要解决,为此,让我们重新设计您的程序:

Design

设计

It is better to resort to an other method you call inside __init__()to draw your GUI. Let us call it initialize_user_interface().

最好使用您在内部调用的其他方法__init__()来绘制 GUI。让我们称之为initialize_user_interface()

When it comes to PassCheck(), you need first to past the object itself to this method. This means the first argument to pass to this method is self. And that is the only argument we need in fact PassCheck(self)because you can access from this method the remaining argument you passed uselessly to it.

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

Program

程序

So here is the full program you need:

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

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

Demo

演示

Here is a screenshot of the running program:

下面是运行程序的截图:

enter image description here

在此处输入图片说明