Tkinter has many inbuilt functions that power the application logic to make it more functional and maintainable. Tkinter has the wait_window() method which ideally waits for an event to happen and executes the event of the main window. The wait_window() method can be called after the event which needs to happen before the main window event.
The wait_window() method is useful in many applications where a particular event needs to be executed first before the main program.
In this example, we have created a toplevel window, which when gets destroyed, the event in the main window gets executed instantly.
# Import the required libraries from tkinter import * # Create an instance of tkinter frame win= Tk() # Set the size of the tkinter window win.geometry("700x350") # Add a Text widget in a toplevel window top= Toplevel(win) top.geometry("450x150") Label(top,text="This is a TopLevel Window", font= ('Aerial 17')).pack(pady=50) # Wait for the toplevel window to be closed win.wait_window(top) print("Top Level Window has been Closed!") win.destroy() win.mainloop()
Running the above code will display a Label text in a top-level window. The Main window waits for the top level window to get destroyed.
Once we close the top level window, the Main window closes too and prints a message on the screen.
Top Level Window has been Closed!