How to close the Window in Tkinter [5 easy Ways]

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
5 min

banner

# Table of Contents

  1. How to close the Window in Tkinter
  2. Using root.destroy vs root.quit in Tkinter
  3. Closing the Tkinter Window by clicking the X button
  4. Closing the Tkinter window when the Escape key is pressed

# How to close the Window in Tkinter

You can use the root.destroy() method to close the Window in Tkinter.

The method destroys all widgets and exits the mainloop.

main.py
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()

close tkinter window

The code for this article is available on GitHub

We used the Tk() class to create a new top-level widget and stored the result in the root variable.

main.py
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.

main.py
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.

Any code after the call to 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.

main.py
from tkinter import Tk, ttk root = Tk() ttk.Button( root, text="Close Tkinter Window", command=root.destroy ).pack() root.mainloop()

close tkinter window simplified version

The code for this article is available on GitHub

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.

main.py
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()

close tkinter window with custom function

The code for this article is available on GitHub

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.

tkinter window closed custom function

If you want to close the Tkinter window and end the execution of the Python program, you have to call:

  1. root.destroy() - destroys all widgets and closes the main loop.
  2. exit() - ends the execution of the Python program.
main.py
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()

root destroy and exit

The code for this article is available on GitHub

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.

main.py
from tkinter import Tk, ttk root = Tk() ttk.Button( root, text="Close Tkinter Window", command=root.destroy ).pack() root.mainloop() print('bobbyhadz.com')

code after root mainloop still runs

However, you wouldn't be able to access any widgets after calling root.destroy.

# Using root.destroy vs root.quit in Tkinter

If 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.

main.py
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())

using root quit instead of root destroy

The code for this article is available on GitHub

If you look at your terminal after clicking the Quit button, you can see that the entry.get() line has run successfully.

entry get line 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:

main.py
# ๐Ÿ‘‡๏ธ Using root.quit() def quit_tk(): root.quit()

To:

main.py
# ๐Ÿ‘‡๏ธ Using root.destroy() def quit_tk(): root.destroy()

Now, I'll run the script with python main.py.

using root destroy instead of root quit

The code for this article is available on GitHub

Closing the Tkinter window works in a similar way, however, if you look at your terminal Window, the following error is raised:

  • _tkinter.TclError: invalid command name ".!entry"

tkinter tclerror invalid command name

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.

# Closing the Tkinter Window by clicking the X button

You can also close the Tkinter window by clicking the X button in the top right corner of the application.

main.py
from tkinter import Tk, ttk root = Tk() ttk.Button( root, text="Close Tkinter Window", command=root.destroy ).pack() root.mainloop()

close tkinter window by clicking x button

The code for this article is available on GitHub

Clicking on the X icon in the top right corner instantly closes the Tkinter window.

# Closing the Tkinter window when the Escape key is pressed

If you need to close the Tkinter window when the Escape key is pressed, use the root.bind() method.

main.py
from tkinter import Tk root = Tk() root.title = 'bobbyhadz.com' def close_window(_event): root.destroy() root.bind('<Escape>', close_window) root.mainloop()

close tkinter window when escape key is pressed

The code for this article is available on GitHub

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.

main.py
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()
  1. root.destroy() - destroys all widgets and closes the main loop.
  2. 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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev