调用该函数时,登录窗口不会关闭。错误: “在destroyLogWin中 LgW.destroy() NameError:名称“ LgW”未定义”
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
预期登录对话框将关闭
答案 0 :(得分:0)
您的问题是LgW
是一个局部变量,这意味着您只能在loginPopup
函数内部访问此变量。
我看到您已经在所有函数中放入了参数self
。如果您使用Class
并只是忘记为我们提供类定义,请使用LgW
替换所有self.LgW
。 self
将使te变量在类中的所有函数中可用。
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`
否则,如果您不想使用类,请使用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
global
关键字将全局设置所有指定的变量,这意味着您将能够从其他函数访问它们。