skip to Main Content

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:UsersIMRANDesktoppyappsblood_donors_admin_dashboardlogin.py", line 88, in login
    messagebox.showerror("Error", "Invalid Email/Password!", parent=self.root)
  File "C:UsersIMRANAppDataLocalProgramsPythonPython310libtkintermessagebox.py", line 98, in showerror
    return _show(title, message, ERROR, OK, **options)
  File "C:UsersIMRANAppDataLocalProgramsPythonPython310libtkintermessagebox.py", line 76, in _show
    res = Message(**options).show()
  File "C:UsersIMRANAppDataLocalProgramsPythonPython310libtkintercommondialog.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:UsersIMRANAppDataLocalProgramsPythonPython310libtkinter__init__.py", line 1921, in __call__
    return self.func(*args)
  File "c:UsersIMRANDesktoppyappsblood_donors_admin_dashboardlogin.py", line 91, in login
    messagebox.showerror("Error", f"Error due to : {str(ex)}", parent=self.root)
  File "C:UsersIMRANAppDataLocalProgramsPythonPython310libtkintermessagebox.py", line 98, in showerror
    return _show(title, message, ERROR, OK, **options)
  File "C:UsersIMRANAppDataLocalProgramsPythonPython310libtkintermessagebox.py", line 76, in _show
    res = Message(**options).show()
  File "C:UsersIMRANAppDataLocalProgramsPythonPython310libtkintercommondialog.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)

2

Answers


  1. Chosen as BEST ANSWER

    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
    

  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search