1
votes

I have a tkinter application with two windows: A MainWindow that is created on startup, and a ChildWindow which is opened after clicking a button.

The ChildWindow should close itself if the user presses a button. However, when I try calling frame.quit, it terminates the entire application instead.

import tkinter as tk

class ChildWindow:
    def __init__(self, master):
        self.top = tk.Toplevel(master)
        self.frame = tk.Frame(self.top)
        self.frame.pack()
        # BUG: Clicking "Close" will fully exit application
        self.close_button = tk.Button(self.frame, text="Close", command=self.frame.quit)
        self.close_button.pack()

class MainWindow:
    def __init__(self, master):
        self.master = master
        self.frame = tk.Frame(self.master)
        self.about_button = tk.Button(self.frame, text="Open child window", command=self._open_child_window)
        self.about_button.pack()
        self.frame.pack()

    def _open_child_window(self):
        self.about_window = ChildWindow(self.master)

root = tk.Tk()
lf = MainWindow(root)
root.mainloop()

Screenshots:

MainWindow ChildWindow

Why does frame.quit exit my application? How can I close the child window without exiting?

1
You're using the wrong method. Use destroy instead of quit. - Aran-Fey
I think this question should be the one that it is duplicate of now, as this is easier to understand. - Nae
Run import tkinter as tk; r = tk.Tk(); help(r.quit); and import tkinter as tk; r = tk.Tk(); help(r.destroy); to see the docstring of the methods. - Nae
@Nae It appears both questions should be marked as duplicates of How do I close a tkinter window? - Stevoisiak

1 Answers

3
votes

It is because quit causes mainloop to exit. With no event loop running, there's nothing left to keep the main window alive.

If you want to close a child window, call the destroy method.

self.close_button = tk.Button(..., command=self.top.destroy)