- Python Tkinter Button Close Window: A Guide to Closing Tkinter Windows with Buttons
- Using the destroy() method to close a Tkinter window
- Creating a button that closes the window
- Open and Close windows in Tkinter
- Defining a function to close the window
- Other ways to close a Tkinter window
- Binding keys to perform certain tasks
- Other quick code examples for closing a Tkinter window with a button
- Conclusion
- Python tkinter windows close
- # Table of Contents
- # How to close the Window in Tkinter
- # Using root.destroy vs root.quit in Tkinter
- How to close a window in Tkinter
- Destroy Function
- Quit Function
Python Tkinter Button Close Window: A Guide to Closing Tkinter Windows with Buttons
In this guide, we explore multiple ways to close a Tkinter window with a button in Python, including using the destroy() method, creating a button that closes the window, defining a function, and binding keys. Learn how to optimize your Tkinter window-closing process today!
- Using the destroy() method to close a Tkinter window
- Creating a button that closes the window
- Open and Close windows in Tkinter
- Defining a function to close the window
- Other ways to close a Tkinter window
- Binding keys to perform certain tasks
- Other quick code examples for closing a Tkinter window with a button
- Conclusion
- How do I close a Tkinter window?
- How do you add an exit button in Python?
- How do I close a current window in Python?
- How do you destroy a window in Python?
When building a GUI application in Python, the ability to close windows with a button is an essential feature. Tkinter is a standard GUI library for Python that has been around for many years. It is easy to learn and use, making it suitable for beginners. In this blog post, we will explore how to close a tkinter window with a button in Python.
Using the destroy() method to close a Tkinter window
The destroy() method can be used to close any available widget as well as with the main tkinter window. This method is convenient because it releases all resources allocated to the widget, making it suitable for closing windows.
Here is an example code snippet that demonstrates how to use the destroy() method to close a Tkinter window:
It is best practice to use the destroy() method to close windows because it ensures that all resources that have been allocated to the window are properly released.
Creating a button that closes the window
To create a button that closes the window, assign root.quit() or root.destroy() to the command attribute of the button. Here is an example code snippet that demonstrates how to create a button that closes the window:
button = Button(root, text="Close", command=root.destroy)
Creating a button to close a window is simple. All you need to do is assign root.destroy() to the command attribute of the button. Here is a cheatsheet that summarizes creating a button to close a window:
button = Button(root, text="Close", command=root.destroy)
Open and Close windows in Tkinter
About this video:In this video, you will learn how to open and close a Tkinter window on a Duration: 4:59
Defining a function to close the window
Defining a function to close the window and calling it through a button is another approach. This method is useful if you want to perform additional tasks before closing the window. Here is an example code snippet that demonstrates how to define a function to close the window:
def close_window(): root.destroy()button = Button(root, text="Close", command=close_window)
In this example, we defined a function called close_window() that calls the destroy() method. We then assigned this function to the command attribute of the button.
Other ways to close a Tkinter window
There are other ways to close a Tkinter window. For example, you can click the “X” button on the window to close it. To disable the “X” button, use the protocol() method.
Here is an example code snippet that demonstrates how to use the protocol() method:
root.protocol("WM_DELETE_WINDOW", root.destroy)
The TopLevel window can be closed by using the “close” button or the destroy() method. Here is an example code snippet that demonstrates how to close a TopLevel window:
Binding keys to perform certain tasks
Keys can be bound to perform certain tasks or events in the application. For example, the Esc key can be bound to close the application window. Here is an example code snippet that demonstrates how to bind the Esc key to close the application window:
Other quick code examples for closing a Tkinter window with a button
In Python , for instance, Python tkinter quit button code sample
# Python program to create a close button # using destroy Non-Class method# Button for closing exit_button = Button(root, text="Exit", command=root.destroy) exit_button.pack(pady=20)
In Python , for example, how to manually close tkinter window code example
In Python case in point, python tkinter how to close a window code sample
Conclusion
Closing a Tkinter window with a button in Python is simple and can be done in multiple ways. The destroy() method can be used with any available widget as well as with the main tkinter window. To create a button that closes the window, assign root.quit() or root.destroy() to the command attribute of the button. Defining a function to close the window and calling it through a button is another approach. Clicking the “X” button on the window will also close it, and the TopLevel window can be closed by using the “close” button or the destroy() method. Keys can be bound to perform certain tasks or events in the application.
Python tkinter windows close
Last updated: Jun 9, 2023
Reading time · 4 min
# Table of Contents
# 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.
Copied!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.
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.
Copied!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.
Copied!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.
Copied!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.
Copied!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.
Copied!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 .
# 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 .
Copied!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.
Copied!# 👇️ Using root.quit() def quit_tk(): root.quit()
How to close a window in Tkinter
There are many different ways to close a Tkinter window. The easiest and simplest way is to click the «X» button on the window. However, this method is rather crude and not very suitable for all situations that you may come across.
Here we’ll be exploring methods which allow you greater control over how your program exits and closes the Tkinter window. We’ll be covering the use of two functions, quit() and destroy() used for this purpose.
Destroy Function
The destroy function will cause Tkinter to exit the mainloop() and simultaneously, destroy all the widgets within the mainloop() .
from tkinter import * def close(): root.destroy() root = Tk() root.geometry('200x100') button = Button(root, text = 'Close the window', command = close) button.pack(pady = 10) root.mainloop()
The window produced by the above code. Clicking the button will destroy the window, causing it to disappear along with any other widgets in it.
You can also optimize the above code by removing the function, and directly having the button call the destroy function on root .
from tkinter import * root = Tk() root.geometry('200x100') button = Button(root, text = 'Close the window', command = root.destroy) button.pack(pady = 10) root.mainloop()
When you use command = root.destroy you pass the method to the Button without the parentheses because you want Button to store the method for future calling, not to call it immediately when the button is created.
Bonus fact, the destroy function works on more than just the Tkinter window. You can use it on individual widgets as well. Try running the following code and see for yourself.
from tkinter import * def destroy(): button.destroy() root = Tk() root.geometry('200x100') button = Button(root, text = 'Close the window', command = destroy) button.pack(pady = 10) root.mainloop()
You can learn more about the destroy() function in it’s own dedicated tutorial.
Quit Function
The quit() function is rather confusing, because of which it’s usually a better (and safer) idea to be using the destroy() function instead. We’ll explain how to use the quit() function to close a tkinter window here anyway.
The quit() function will exit the mainloop, but unlike the destroy() function it does not destroy any existing widgets including the window. Running the below code will simply stop the TCL interpreter that Tkinter uses, but the window (and widgets) will remain.
from tkinter import * root = Tk() root.geometry('200x100') button = Button(root, text = 'Close the window', command = root.quit) button.pack(pady = 10) root.mainloop()
The quit function can cause problems if you call your application from an IDLE. The IDLE itself is a Tkinter application, so if you call the quit function in your app and the TCL interpreter ends up getting terminated, the whole IDLE will also be terminated.
For these reasons, it’s best to use the destroy function.
This marks the end of the Tkinter close window article. Any suggestions or contributions for Coderslegacy are more than welcome. Questions regarding the article can be asked in the comments section below.