How to close only the TopLevel window in Python Tkinter?
Python Tkinter is a popular GUI toolkit that consists of several widgets and options. One of the commonly used widgets is the Toplevel widget, which is used to create additional windows in Tkinter applications. However, while building such applications, users may come across a situation where they want to close only the Toplevel window without affecting the main application window. In this article, we are going to explore how to close only the Toplevel window in Python Tkinter.
Creating a Toplevel Window
To demonstrate how to close a Toplevel window, first, we need to create one. In Python Tkinter, Toplevel widget is used to create any additional window. For example, the following code will create a Toplevel window:
from tkinter import *
root = Tk()
toplevel = Toplevel(root)
We have imported the Tkinter module and created a root window using the Tk() constructor. Then we have created a Toplevel window using the Toplevel() method and assigned it to the “toplevel” variable. Now that we have created a Toplevel window, we can proceed with how to close it when needed.
Closing the Toplevel Window
When we close the Toplevel window, we do not want to exit the entire application. The reason behind this is that when creating an application, the Toplevel window and the Main window are separate entities that interact with each other in various ways. To close only Toplevel window, we can use the “destroy()” method.
from tkinter import *
def close_window():
toplevel.destroy()
root = Tk()
toplevel = Toplevel(root)
button = Button(toplevel, text="Close Window", command=close_window())
button.pack()
root.mainloop()
Here, we have created a function called “close_window()” that will be triggered when the user clicks on the “Close Window” button. This function calls the “destroy()” method of the Toplevel window, which will close the window without affecting the main application window.
Understanding destroy() Function
The “destroy()” method is a built-in method of the Toplevel widget that is used to remove any widget from the application. When we call this method, it completely destroys the widget and removes it from the application. When we destroy a Toplevel widget, all the widgets associated with it are also destroyed.
toplevel.destroy()
Using the protocol() Method
Another way to close the Toplevel window is by using the protocol() method. This method is useful when we want to close the window on certain events, such as closing the window manually or pressing the “X” button on the window. To achieve this, we can use the “WM_DELETE_WINDOW” protocol, which is triggered when we try to close the window.
from tkinter import *
def close_window():
toplevel.destroy()
root = Tk()
toplevel = Toplevel(root)
toplevel.protocol("WM_DELETE_WINDOW", close_window)
button = Button(toplevel, text="Close Window", command=close_window)
button.pack()
root.mainloop()
Here, we have created a function called “close_window()” that will be triggered when the user tries to close the Toplevel window. We have used the “protocol()” method to set it as a protocol for the “WM_DELETE_WINDOW” event. When the user tries to close the Toplevel window, the “close_window()” function is triggered, which will destroy the Toplevel window.
Conclusion
In this article, we have explored how to close only the Toplevel window in Python Tkinter applications. We have used the “destroy()” method and the “protocol()” method to close the Toplevel window. We hope this article has helped you in understanding how to close only the Toplevel window without affecting the main application window.