How to detect if a child window is opened or closed in tkinter
You can create a reference to if the window is closed or not, then continue, like:
opened = False # Create a flag
# First Window
def open_first_win():
global opened
if not opened:
first_win = Toplevel(root)
first_win.title("First Window")
opened = True # Now it is open
first_win.protocol('WM_DELETE_WINDOW',lambda: onclose(first_win)) # If it is closed then set it to False
def onclose(win): # Func to be called when window is closing, passing the window name
global opened
opened = False # Set it to close
win.destroy() # Destroy the window
# Second Window
def open_second_win():
global opened
if not opened:
second_win = Toplevel(root)
second_win.title("Second Window")
opened = True
second_win.protocol('WM_DELETE_WINDOW',lambda: onclose(second_win))
As the word stands, protocol
just adds some rules to the windows, like WM_DELETE_WINDOW
means what function to call, when the window is tried to close.
Why does closing a tkinter child window with `frame.quit` exit my application?
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)
Related Topics
Python: Plotting Percentage in Seaborn Bar Plot
Printing Lists in Python Without Spaces
How to Check List Containing Nan
How to Constantly Run Python Script in the Background on Windows
Check Json Data Is None in Python
Bold Formatting in Python Console
How to Get Maximum Length of Each Column in the Data Frame Using Pandas Python
How to Calculate Average a Dictionary from List of Dictionary Data
How to Check Whether a Number Is Divisible by Another Number
How to Remove/Delete a Virtualenv
How to Install Pip for a Specific Python Version
Pandas Merge - How to Avoid Duplicating Columns
Printing the Number of Days in a Given Month and Year [Python]
How to Install Python Packages from the Tar.Gz File Without Using Pip Install
How to Start a Background Process in Python
Comparing Two Json Objects Irrespective of the Sequence of Elements in Them
Pyspark Regexp_Replace With List Elements Are Not Replacing the String