Home > OS >  _tkinter.TclError: can't invoke "tk_messageBox" command: application has been destroy
_tkinter.TclError: can't invoke "tk_messageBox" command: application has been destroy

Time:09-05

When i execute this code it works fine to login but when i logout and then come again to the login window, then close login window it closes but shows this exception in terminal of visual studio code.

    Exception in Tkinter callback
Traceback (most recent call last):
  File "c:\Users\IMRAN\Desktop\pyapps\blood_donors_admin_dashboard\login.py", line 88, in login
    messagebox.showerror("Error", "Invalid Email/Password!", parent=self.root)
  File "C:\Users\IMRAN\AppData\Local\Programs\Python\Python310\lib\tkinter\messagebox.py", line 98, in showerror
    return _show(title, message, ERROR, OK, **options)
  File "C:\Users\IMRAN\AppData\Local\Programs\Python\Python310\lib\tkinter\messagebox.py", line 76, in _show
    res = Message(**options).show()
  File "C:\Users\IMRAN\AppData\Local\Programs\Python\Python310\lib\tkinter\commondialog.py", line 45, in show
    s = master.tk.call(self.command, *master._options(self.options))
_tkinter.TclError: can't invoke "tk_messageBox" command: application has been destroyed

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\IMRAN\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 1921, in __call__
    return self.func(*args)
  File "c:\Users\IMRAN\Desktop\pyapps\blood_donors_admin_dashboard\login.py", line 91, in login
    messagebox.showerror("Error", f"Error due to : {str(ex)}", parent=self.root)
  File "C:\Users\IMRAN\AppData\Local\Programs\Python\Python310\lib\tkinter\messagebox.py", line 98, in showerror
    return _show(title, message, ERROR, OK, **options)
  File "C:\Users\IMRAN\AppData\Local\Programs\Python\Python310\lib\tkinter\messagebox.py", line 76, in _show
    res = Message(**options).show()
  File "C:\Users\IMRAN\AppData\Local\Programs\Python\Python310\lib\tkinter\commondialog.py", line 45, in show
    s = master.tk.call(self.command, *master._options(self.options))
_tkinter.TclError: can't invoke "tk_messageBox" command: application has been destroyed

NOTE: Line 88 is the last else part before except code block.

def login(self):
    email = self.user.get()
    password = self.passwd.get()
    if email == "" and password == "":
        messagebox.showerror("Error", "Please Enter All The Fields!", parent=self.root)
    elif email == "":
        messagebox.showerror("Error", "Please Enter Email Address!", parent=self.root)
    elif password == "":
        messagebox.showerror("Error", "Please Enter Password!", parent=self.root)
    else:
        ref = db.reference('users')
        data = ref.get()
        
        for key, val in data.items():
             
            self.loguser.update({key:val})
        try:
            for user in self.loguser.values():
                if user['email'] == email and user['password'] == password and user['admin'] == True:
                    self.root.destroy()
                    os.system('python main.py')
            else:
                messagebox.showerror("Error", "Invalid Email/Password!", parent=self.root)
          
        except Exception as ex:
            messagebox.showerror("Error", f"Error due to : {str(ex)}", parent=self.root)

CodePudding user response:

Finally after trying again and again I have solved the problem. @Thingamabobs point out the for loop problem. So i try to control the for loop after destroying root window by adding break in for loop after login. Here Are the code changes:

 for user in self.loguser.values():
     if user['email'] == email and user['password'] == password and user['admin'] == True:
         self.root.destroy()
         os.system('python main.py')
         break

CodePudding user response:

This error happens because you used the destroy() function. Just don't destroy it. If you want it to be hidden then use withdraw() which hides your root window and then deiconify() to show it again if you want. In this way the window is still there but it cannot be found by the user.

A simple modification of your code

def login(self):
    email = self.user.get()
    password = self.passwd.get()
    if email == "" and password == "":
        messagebox.showerror("Error", "Please Enter All The Fields!", parent=self.root)
    elif email == "":
        messagebox.showerror("Error", "Please Enter Email Address!", parent=self.root)
    elif password == "":
        messagebox.showerror("Error", "Please Enter Password!", parent=self.root)
    else:
        ref = db.reference('users')
        data = ref.get()
        
        for key, val in data.items():
             
            self.loguser.update({key:val})
        try:
            for user in self.loguser.values():
                if user['email'] == email and user['password'] == password and user['admin'] == True:
                    self.root.withdraw()
                    os.system('python main.py')
                else:
                    messagebox.showerror("Error", "Invalid Email/Password!", parent=self.root)
          
        except Exception as ex:
            messagebox.showerror("Error", f"Error due to : {str(ex)}", parent=self.root)

Hope this helps

  • Related