BayesianMonk
BayesianMonk

Reputation: 647

Why Tkinter application does not close after call to exit

I am developping an application with tkinter. It is made of several frame (windows) put one onto the other. I can pass from one window to the other with a push button. I built it upon an example I saw here on Stack overflow, but I cannot recall the link to it.

I suspect that due to this choice I made, the tkinter does not close properly after I call the method self.quit(). The example below is a simplified version of my app, although it has the same structure than the full app.

With the code below, you can see that the application won't close before the end of the full program, i.e. after the 10 sec delay I put.

import tkinter as tk
import tkinter.ttk as ttk
import time


class App:
    """
    Inherited from the Frame class
    """

    def __init__(self):
        self.root = tk.Tk()
        self.root.title('Test')

        # This frame will contain all windows
        container = ttk.Frame(self.root)
        container.pack(side="top", fill="both", expand=True)

        # Create different pages/windows of the application
        self.frames = {}
        self.frames["Window1"] = Window1(parent=container, controller=self)
        self.frames["Window2"] = Window2(parent=container, controller=self)
        self.frames["Window3"] = Window3(parent=container, controller=self)

        self.frames["Window1"].grid(row=0, column=0, sticky="nsew")
        self.frames["Window2"].grid(row=0, column=0, sticky="nsew")
        self.frames["Window3"].grid(row=0, column=0, sticky="nsew")

        self.show_frame("Window1")

    def run(self):
        self.root.deiconify
        self.root.mainloop()

    def show_frame(self, page_name):
        '''
        Show a frame for the given page name
        '''
        frame = self.frames[page_name]
        frame.tkraise()


class Window1(ttk.Frame):
    def __init__(self, parent, controller):
        ttk.Frame.__init__(self, parent)
        self.controller = controller
        self.columnconfigure(0, weight=1)
        self.rowconfigure(0, weight=1)

        # Go to window 2
        self.buttonW2 = ttk.Button(self, text='Go to 2', command=lambda: controller.show_frame("Window2"))
        self.buttonW2.grid(row=0, column=0, sticky='NSE')

        # Go to window 3
        self.buttonW3 = ttk.Button(self, text='Go to 3', command=lambda: controller.show_frame("Window3"))
        self.buttonW3.grid(row=0, column=1, sticky='NSE')

        # EXIT button
        self.buttonExit = ttk.Button(self, text='Exit', command=self.quit)
        self.buttonExit.grid(row=0, column=2, sticky='NSE')


class Window2(ttk.Frame):
    def __init__(self, parent, controller):
        ttk.Frame.__init__(self, parent)
        self.controller = controller
        self.columnconfigure(0, weight=1)
        self.rowconfigure(0, weight=1)
        # Go back button
        buttonBack = ttk.Button(self, text="Back to 1", command=lambda: controller.show_frame("Window1"))
        buttonBack.grid(row=0, column=3, sticky="NSE")


class Window3(ttk.Frame):
    def __init__(self, parent, controller):
        ttk.Frame.__init__(self, parent)
        self.controller = controller
        self.columnconfigure(0, weight=1)
        self.rowconfigure(0, weight=1)
        # Go back button
        buttonBack = ttk.Button(self, text="Back to 1", command=lambda: controller.show_frame("Window1"))
        buttonBack.grid(row=1, column=1, sticky="E")


if __name__ == '__main__':
    app = App()
    app.run()

    print('The App should be closed')

    time.sleep(10)

    print('The App is now closed')

Does anyone can help me for this?

Thanks

Upvotes: 1

Views: 1957

Answers (1)

Nummer_42O
Nummer_42O

Reputation: 432

Tkinters quit command just stops the mainloop but doesn't destroy the window. If you want the window to be removed use destroy.

So create the exitbutton like:

    self.buttonExit = ttk.Button(self, text='Exit', command=self.controller.root.destroy)
    self.buttonExit.grid(row=0, column=2, sticky='NSE')

Upvotes: 2

Related Questions