关闭Tkinter GUI中的窗口按钮点击

2024-10-03 17:27:06 发布

您现在位置:Python中文网/ 问答频道 /正文

调用函数时,登录窗口不会关闭。错误: “在洛格温 LgW.销毁() 名称错误:未定义名称“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

应关闭登录对话框窗口


Tags: textself名称def错误logincanceltk
1条回答
网友
1楼 · 发布于 2024-10-03 17:27:06

您的问题是LgW是一个局部变量,这意味着您只能在loginPopup函数中访问这个变量。 我看到您在所有函数中都使用了参数self。如果您使用了一个Class而忘记提供类定义:用self.LgW替换所有的LgWself将使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关键字将所有指定的变量设置为全局变量,这意味着您可以从其他函数访问它们。你知道吗

相关问题 更多 >