Mariana
Mariana

Reputation: 81

How to close the whole GUI when toplevel is closed tkinter

I'm writing a tkinter app that creates a class with multiple toplevels. I need to be able to close the whole gui when any of the X button in the toplevels are pressed. How can I do this?

def main():
    root = tk.Tk()
    app = Example(master=root)
    app.mainloop()

class Example(tk.Frame):
    def __init__(self, master):
        self.master = master
        super().__init__(master)
        self.initUI()

 def initUI(self):
        self.master.withdraw()
        self.initUIL = tk.Toplevel(self.master)

        self.initUIL.title('Init')
        self.pack(fill = tk.BOTH, expand=1)

        frame1 = tk.Frame(self.initUIL)

       #I need to close the whole gui when the x in this toplevel is pressed

Upvotes: 0

Views: 336

Answers (1)

Mariana
Mariana

Reputation: 81

I solved it, it was pretty easy, you need to change the protocol for each toplevel.

self.toplevel.protocol("WM_DELETE_WINDOW", self.ask_quit)

def ask_quit():
    MsgBox = tk.messagebox.askquestion ('Quit',"Are you sure you want to quit?")
    if MsgBox == 'yes':
       self.master.destroy()

Upvotes: 2

Related Questions