[Python] Why does closing a tkinter child window with `frame.quit` exit my application?...

Discussão em 'Python' iniciado por Stack, Maio 14, 2025.

  1. Stack

    Stack Membro Participativo

    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:

    [​IMG] [​IMG]

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

    Continue reading...

Compartilhe esta Página