Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

tkinter

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()
like image 814
DrDevilHell Avatar asked Jul 06 '16 17:07

DrDevilHell


1 Answers

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, NEVER run 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:

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

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.

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

like image 183
Billal Begueradj Avatar answered Oct 27 '22 10:10

Billal Begueradj