Python Forum

Full Version: How to exit when multiple root windows.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
What is the correct way to exit something like this (simplified version)

class Main (object):
    def __init__(self):
        self.userGUI = Tk(basename="userGUIs")
        self.progressGUI = Tk(basename="progressGUIs")

        self.uiGUI = guiA(self, self.userGUI)
        self.uiGUI.pack()
        self.progGUI = guiB(self, self.progressGUI)
        self.progGUI.pack()

        self.uiGUI.doCode()

    def doSomeOtherStuff():
        # Initiate a progress bar while this is happening and a new frame for the userGUI is being built
        self.progGUI.doProgress()
        # Run some calculations
        self.progGUI.iterateProgress()

class guiA(tk.Frame):
    def __init__(self, parent, gui):
        tk.Frame.__init__(self,gui)

    def doCode(self):
        # Code to build GUI widgets, etc.
        # When submit button clicked do below
        self.parent.doSomeOtherStuff()

class guiB(tk.Frame):
    def __init__(self, parent, gui):
        tk.Frame.__init__(self,gui)

    def doProgress(self):
        # build progress bar

    def iterateProgress(self):
        # update progressbar
Now, I've left out the mainloop(), destroy(), and quit() methods because I'd like someone to show me exactly where they are supposed to go to be sure that all traces of all GUIs exit and are destroyed cleanly when the program completes. Do I need two mainloops? Do I need to 'destroy()' or 'quit()' both GUIs?
You can have only one root window, but as many TopLevel windows as you wish
if you want to stop all, delete the real root instance, and if necessary run sys.exit (which requires sys import)
If you just want to delete individual windows, add a quit method in the code for that window and just delete it's TopLevel instance.
Would it be painful or too much to ask for you to show me how you would modify the above code, or just a sample of what I am describing below?

Essentially, I have a user GUI where I keep the same canvas and wipe the frame clean each time I prompt a user for more input. While the new frames are being built I have a progress bar GUI that pops up so I didn't want to use the same GUI for each.
Ok, so this is how I understand your suggestion. Changes and additions in red.

class Main (object):
    def __init__(self):
        ########## CHANGED ############
        self.rootGUI = Tk(basename="myProgram")
        self.userGUI = Toplevel(self.rootGUI)
        self.progressGUI = Toplevel(self.rootGUI)
 
        self.uiGUI = guiA(self, self.userGUI)
        self.uiGUI.pack()
        self.progGUI = guiB(self, self.progressGUI)
        self.progGUI.pack()
 
        self.uiGUI.doCode()

        ########## ADDITION ############
        self.rootGUI.mainloop()
 
    def doSomeOtherStuff():
        # Initiate a progress bar while this is happening and a new frame for the userGUI is being built
        self.progGUI.doProgress()
        # Run some calculations
        self.progGUI.iterateProgress()
    
        ########## ADDITION ############
        self.rootGUI.destroy()
 
class guiA(tk.Frame):
    def __init__(self, parent, gui):
        tk.Frame.__init__(self,gui)
 
    def doCode(self):
        # Code to build GUI widgets, etc.
        # When submit button clicked do below
        self.parent.doSomeOtherStuff()
 
class guiB(tk.Frame):
    def __init__(self, parent, gui):
        tk.Frame.__init__(self,gui)
 
    def doProgress(self):
        # build progress bar
 
    def iterateProgress(self):
        # update progressbar
This code won't run as presented.
Please edit and add missing code