简体   繁体   中英

Problem Loading The Tkinter window for second time

So I'm trying to make the main menu for a Tkinter app I've developed but here's the problem when I click one on the button to open the other file which is the other window it's ok but when I close that window and try to open it again by clicking on the same button in the main menu I get this error:

Exception in Tkinter callback
Traceback (most recent call last):
  File "c:\users\alireza\anaconda3\Lib\tkinter\__init__.py", line 1702, in __call__
    return self.func(*args)
  File "menu.py", line 32, in open_main
    root.destroy()
  File "c:\users\alireza\anaconda3\Lib\tkinter\__init__.py", line 2059, in destroy
    self.tk.call('destroy', self._w)
_tkinter.TclError: can't invoke "destroy" command: application has been destroyed

and this is my main menu file (menu.py):

from tkinter import *
import tkinter.messagebox


class Application:
    def __init__(self, master, *args, **kwargs):


        self.mainframe = Frame(master, width = 300, height = 400, bg = 'slategray1')
        self.mainframe.pack()

        self.main_button = Button (self.mainframe, width = 25, height = 2,bg = "SteelBlue2" , text = "Customer Service", command = self.open_main)
        self.main_button.place(x= 55, y =50 )

        self.add_to_db_button = Button (self.mainframe, width = 25, height = 2,bg = "SteelBlue2" , text = "Add Item To Inventory", command = self.open_add_to_db)
        self.add_to_db_button.place(x= 55, y =100 )

        self.update_button = Button (self.mainframe, width = 25, height = 2,bg = "SteelBlue2" , text = "Update Inventory Items", command = self.open_update)
        self.update_button.place(x= 55, y =150 )

        self.about_button = Button (self.mainframe, width = 25, height = 2,bg = "sea green" , text = "Credits", command = self.about)
        self.about_button.place(x= 55, y =300 )

        self.close_button = Button (self.mainframe, width = 25, height = 2,bg = "navajo white" , text = "Quit", command = self.close_window)
        self.close_button.place(x= 55, y =350 )

    def close_window(self, *args, **kwargs):
        root.destroy()

    def open_main(self, *args, **kwargs):
        import main
        root.destroy()

    def open_add_to_db(self, *args, **kwargs):
        import add_to_db
        root.destroy()

    def open_update(self, *args, **kwargs):
        from update import AppUpdate
        root.destroy()

    def about(self, *args, **kwargs):
        tkinter.messagebox.showinfo("About Me", "Programmed and designed by Alireza Bagheri.")


root = Tk()
root.title("Main Menu")
root.resizable(False, False)
b = Application(root)
root.geometry("301x401+0+0")
root.mainloop()

I don't exactly know where the problem is so it would mean a lot to point me in the right direction

As Idlehands has commented, I don't think that you should rely on import main to build a window, but instead define a function ( build_window() perhaps?) within main and then you can call it such as main.build_window() . This will ensure you know what code is supposed to execute, and may help avoid errors. I tried running your code, and could not replicate the error but instead encountered errors of:

  1. ImportError: No module named 'main'
  2. ImportError: No module named 'update'
  3. ImportError: No module named 'add_to_db'

So this is not a minimum, verifiable, and complete example code of the error you are facing. The most we can do to help you is to speculate a solution. My speculation is if you want to "hide" a window that you will later return to, you should .iconify() it which will minimize it to the taskbar. If you call .destroy() on a Tkinter widget that widget will be deleted from memory, and if you would like another you will have to define it and .pack() , .place() , or .grid() it back onto the screen! I suggest you read https://stackoverflow.com/help/mcve to get an idea of what a minimal, complete, and verifiable example is. Fixing your question would make it much easier for the community to help you with it.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM