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()
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:
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.
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()
Here is a screenshot of the running program:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With