Last updated: Apr 10, 2024
Reading timeยท5 min
root.destroy
vs root.quit
in TkinterX
buttonYou can use the root.destroy()
method to close the Window in Tkinter.
The method destroys all widgets and exits the mainloop.
from tkinter import Tk, ttk root = Tk() frm = ttk.Frame(root, padding=10) frm.grid() ttk.Label(frm, text="bobbyhadz.com").grid(column=0, row=0) # ๐๏ธ Close tkinter window when the button is clicked ttk.Button(frm, text="Close Window", command=root.destroy).grid(column=1, row=0) root.mainloop()
We used the Tk()
class to create a new top-level widget and stored the result
in the root
variable.
root = Tk()
We then used the ttk.Button()
class to create a Button widget.
The command
keyword argument we passed to the ttk.Button
class is a method
that gets called when the button is clicked.
ttk.Button(frm, text="Close Window", command=root.destroy).grid(column=1, row=0)
The root.destroy()
method destroys all widgets and exits the mainloop.
root.mainloop()
will still run, however, attempts to access any widgets after calling root.destroy()
will fail because the widgets no longer exist.Here is a simplified version without a Label
widget.
from tkinter import Tk, ttk root = Tk() ttk.Button( root, text="Close Tkinter Window", command=root.destroy ).pack() root.mainloop()
As shown in the short clip, the root.destroy()
method exits, stops the
mainloop()
and closes the Tkinter program completely.
After calling the method, the window and all widgets are destroyed.
You can also define a custom function that closes the Tkinter window.
from tkinter import Tk, ttk root = Tk() frm = ttk.Frame(root, padding=10) frm.grid() ttk.Label(frm, text="bobbyhadz.com").grid(column=0, row=0) # ๐๏ธ defining a custom function def quit_tk(): root.destroy() print('Tkinter window closed โ ') # ๐๏ธ Close tkinter window when the button is clicked ttk.Button(frm, text="Close Window", command=quit_tk).grid(column=1, row=0) root.mainloop()
The quit_tk()
function is called when the user clicks the "Close Window"
button.
Defining a custom wrapper function enables you to run some custom code after closing the Tkinter window.
The example simply prints a message to the terminal.
If you want to close the Tkinter window and end the execution of the Python program, you have to call:
root.destroy()
- destroys all widgets and closes the main loop.exit()
- ends the execution of the Python program.from tkinter import Tk, ttk root = Tk() frm = ttk.Frame(root, padding=10) frm.grid() ttk.Label(frm, text="bobbyhadz.com").grid(column=0, row=0) # ๐๏ธ defining a custom function def quit_tk(): # 1) destroys all widgets and closes the main loop root.destroy() # 2) ends the execution of the Python program exit() # ๐๏ธ Close tkinter window when the button is clicked ttk.Button(frm, text="Close Window", command=quit_tk).grid(column=1, row=0) root.mainloop()
If you have any code that is placed after the root.mainloop()
line and you
close the window with root.destroy()
method, the code will still run.
from tkinter import Tk, ttk root = Tk() ttk.Button( root, text="Close Tkinter Window", command=root.destroy ).pack() root.mainloop() print('bobbyhadz.com')
However, you wouldn't be able to access any widgets after calling
root.destroy
.
root.destroy
vs root.quit
in TkinterIf you need to be able to interact with the widgets after, you have to use the
root.quit()
method.
When the root.quit()
method is used, the interpreter is still intact, as are
all the widgets.
In other words, you can still interact with the widgets after calling
root.quit()
.
Here is a simple example of using the root.quit()
method instead of
root.destroy
.
from tkinter import Tk, ttk root = Tk() ttk.Label(root, text="bobbyhadz.com").grid(column=0, row=0) entry = ttk.Entry(root) entry.grid(row=0, column=1) # ๐๏ธ Using root.quit() def quit_tk(): root.quit() ttk.Button(root, text="Quit", command=quit_tk).grid(row=0, column=2) root.mainloop() print(entry.get())
If you look at your terminal after clicking the Quit button, you can see
that the entry.get()
line has run successfully.
The widgets are still accessible even after calling root.quit()
.
This is not the case when you use the root.destroy()
method.
I'll make the following change in the code snippet.
From:
# ๐๏ธ Using root.quit() def quit_tk(): root.quit()
To:
# ๐๏ธ Using root.destroy() def quit_tk(): root.destroy()
Now, I'll run the script with python main.py
.
Closing the Tkinter window works in a similar way, however, if you look at your terminal Window, the following error is raised:
This is because we no longer have access to the widgets after calling
root.destroy()
.
The root.destroy()
method destroys all widgets and exits the mainloop.
Trying to access a widget after calling the method raises the aforementioned error.
X
buttonYou can also close the Tkinter window by clicking the X
button in the top
right corner of the application.
from tkinter import Tk, ttk root = Tk() ttk.Button( root, text="Close Tkinter Window", command=root.destroy ).pack() root.mainloop()
Clicking on the X
icon in the top right corner instantly closes the Tkinter
window.
If you need to close the Tkinter window when the Escape key is pressed, use the
root.bind()
method.
from tkinter import Tk root = Tk() root.title = 'bobbyhadz.com' def close_window(_event): root.destroy() root.bind('<Escape>', close_window) root.mainloop()
We used the root.bind()
method to bind the Escape
key to the close_window
function.
If you also want to end the execution of the Python program, call the exit()
method as well.
from tkinter import Tk, ttk root = Tk() root.title = 'bobbyhadz.com' def close_window(_event): # 1) destroys all widgets and closes the main loop root.destroy() # 2) ends the execution of the Python program exit() root.bind('<Escape>', close_window) root.mainloop()
root.destroy()
- destroys all widgets and closes the main loop.exit()
- ends the execution of the Python program.I've also written an article on how to get the value of an Entry widget in Tkinter.
You can learn more about the related topics by checking out the following tutorials: