The login window does not close when the function is called. The error: "in destroyLogWin LgW.destroy() NameError: name 'LgW' is not defined"
def loginPopup(self):
LgW = tk.Toplevel()
LgW.wm_title("Login")
LgW.geometry('350x150')
LgW.resizable(0,0)
LgW.wm_iconbitmap('icon.ico')
self.usernameLabel = tk.Label(LgW, text="Username")
self.passwordLable = tk.Label(LgW, text="Password")
self.usernameEntry = tk.Entry(LgW)
self.passwordEntry = tk.Entry(LgW, show="*")
self.usernameLabel.grid(row=0,padx=50,pady=15)
self.passwordLable.grid(row=1, padx= 50)
self.usernameEntry.grid(row=0, column=1)
self.passwordEntry.grid(row=1, column=1)
self.logbtn = ttk.Button(LgW, text="Login", command=self.CheckLogin)
self.logbtn.place(relx=0.54, rely=0.73)
self.Cancel_logbtn = ttk.Button(LgW, text="Cancel",command=self.destroyLogWin)
self.Cancel_logbtn.place(relx=0.29, rely=0.73)
def destroyLogWin(self):
LgW.destroy()
def CheckLogin(self):
print("clicked")
if self.usernameEntry.get() == "" and self.passwordEntry.get() == "":
print("approved")
#self.employeeReg()
self.destroyLogWin()
# home()
else:
tk.messagebox.showerror('Logininfo..','Invalid Login\nCheck Username and Password') # show error message
expected the login dialogue window to close
Your issue is that LgW
is a local variable wich means that you can only access this variable inside your loginPopup
function. I see that you've put the parameter self
in all your functions. If you used a Class
and simply forgot to provide us the class definition : replace all your LgW
by self.LgW
. self
will make te variable usable in all of your functions in your class.
Class loginPopup:
def __init__(self):
self.LgW = tk.Toplevel()
self.LgW.wm_title("Login")
self.LgW.geometry('350x150')
self.LgW.resizable(0,0)
self.LgW.wm_iconbitmap('icon.ico')
self.usernameLabel = tk.Label(LgW, text="Username")
self.passwordLable = tk.Label(LgW, text="Password")
self.usernameEntry = tk.Entry(LgW)
self.passwordEntry = tk.Entry(LgW, show="*")
self.usernameLabel.grid(row=0,padx=50,pady=15)
self.passwordLable.grid(row=1, padx= 50)
self.usernameEntry.grid(row=0, column=1)
self.passwordEntry.grid(row=1, column=1)
self.logbtn = ttk.Button(LgW, text="Login", command=self.CheckLogin)
self.logbtn.place(relx=0.54, rely=0.73)
self.Cancel_logbtn = ttk.Button(LgW, text="Cancel",command=self.destroyLogWin)
self.Cancel_logbtn.place(relx=0.29, rely=0.73)
def destroyLogWin(self):
self.LgW.destroy()
def CheckLogin(self):
print("clicked")
if self.usernameEntry.get() == "" and self.passwordEntry.get() == "":
print("approved")
#self.employeeReg()
self.destroyLogWin()
# home()
else:
tk.messagebox.showerror('Logininfo..','Invalid Login\nCheck Username and Password') # show error message`
Otherwise if you don't want to use classes use global
:
def loginPopup(self):
global LgW, usernameEntry, passwordEntry
LgW = tk.Toplevel()
LgW.wm_title("Login")
LgW.geometry('350x150')
LgW.resizable(0,0)
LgW.wm_iconbitmap('icon.ico')
usernameLabel = tk.Label(LgW, text="Username")
passwordLable = tk.Label(LgW, text="Password")
usernameEntry = tk.Entry(LgW)
passwordEntry = tk.Entry(LgW, show="*")
usernameLabel.grid(row=0,padx=50,pady=15)
passwordLable.grid(row=1, padx= 50)
usernameEntry.grid(row=0, column=1)
passwordEntry.grid(row=1, column=1)
logbtn = ttk.Button(LgW, text="Login", command=self.CheckLogin)
logbtn.place(relx=0.54, rely=0.73)
Cancel_logbtn = ttk.Button(LgW, text="Cancel",command=destroyLogWin)
Cancel_logbtn.place(relx=0.29, rely=0.73)
def destroyLogWin():
LgW.destroy()
def CheckLogin():
print("clicked")
if usernameEntry.get() == "" and passwordEntry.get() == "":
print("approved")
destroyLogWin()
# home()
else:
tk.messagebox.showerror('Logininfo..','Invalid Login\nCheck Username and Password') # show error message
the global
keyword will set all the specified variables global which means you will be able to access them from others functions.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.