Python Forum
[Tkinter] How to exit when multiple root windows.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] How to exit when multiple root windows.
#1
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?
Reply
#2
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.
Reply
#3
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.
Reply
#4
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
Reply
#5
This code won't run as presented.
Please edit and add missing code
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Tkinter multiple windows in the same window tomro91 1 790 Oct-30-2023, 02:59 PM
Last Post: Larz60+
  [Tkinter] How to close all windows when exit program scratchmyhead 3 6,317 May-17-2020, 08:19 AM
Last Post: menator01
  [Tkinter] Program with Multiple Windows - how to use Class: Riddle 1 2,937 Apr-09-2020, 08:30 PM
Last Post: Riddle
  Avoid multiple windows of the same kind backermaster 1 2,220 Oct-30-2018, 08:32 AM
Last Post: Gribouillis
  OnExit closes windows, but dosen't exit application Larz60+ 3 4,749 Sep-23-2017, 06:12 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020