How to close tkinter window on button click. pack() #button to close the window root. Jul 25, 2022 · To close Tkinter Button, we can use several ways such as the use of destroy () or quit method. When you click the "Close Window" button, it will call window. To close a tkinter window, we can use the destroy () method. -- will disturb this activity ("while executing" error) rather than just terminate it. Dec 9, 2020 · Creating a GUI using tkinter is an easy task. The destroy () is a universal widget method i. Finally, run the mainloop of the application window. The best solution in almost every case is to use a flag. Apr 10, 2024 · The quit_tk() function is called when the user clicks the "Close Window" button. Next, create a button and call the close () function. Define a function close () to close the window. To close a window in Tkinter programmatically, call destroy method on the Tk class object which is our window. It will destroy the root object of the Tkinter application. destroy () inside close (). In this tutorial, you will learn how to programmatically close the window in Tkinter, with examples. We will learn this with examples based on some scenarios. Given below are various methods by which this can be achieved. . destroy and root. Set the size of the frame using geometry method. after, stopping this activity with destroy() -- even by using protocol (), a button, etc. Method 1: Using destroy () Non-Class method Approach: Import tkinter module. Let's get started with examples. Close Tkinter Button using destroy () method In this example, we created a button and a quit () method called when this button is clicked. Jul 23, 2025 · Prerequisites: Tkinter Python's Tkinter module offers the Button function to create a button in a Tkinter Window to execute any task once the button is clicked. Example: In the below example, we are going to implement the destroy () method using a button. mainloop() Feb 2, 2024 · This tutorial introduces how to close a Tkinter window with a button that has a function or command attached to it. destroy, which will close the window and exit the tkinter main loop. This article demonstrates several approaches for binding a Tkinter button to a function that exits the Python program efficiently and cleanly. You can use this method to close any Tk or Toplevel window in your tkinter application. Here is a simple, silly example of how to use it (although I am certain that most of you don Steps Import the tkinter library and create an instance of tkinter frame. See the Sep 21, 2008 · Depending on the Tkinter activity, and especially when using Tkinter. The task can be assigned in the command parameter of Button () function. quit methods. mainloop (), which keeps the GUI window running. In the context of a Python Tkinter application, developers need a way to terminate the program on the event of a button click. Method 1: Using Aug 19, 2025 · The button's command parameter is set to 'close_window', so clicking the button will execute the "close_window ()" function. Mar 2, 2024 · Problem Formulation: GUI applications often require a clear exit strategy to close the application gracefully. Create a main window from Tkinter import * #use tkinter instead of Tkinter (small, not capital T) if it doesn't work #as it was changed to tkinter in newer Python versions root = Tk() Button(root, text="Quit", command=root. The program starts the Tkinter event loop using parent. e we can use this method with any of the available widgets as well as with the main tkinter window. The solutions include root. Defining a custom wrapper function enables you to run some custom code after closing the Tkinter window. Aug 19, 2025 · The button's command parameter is set to 'close_window', so clicking the button will execute the "close_window ()" function. destroy). Call the method win.
cmkg efy fvkgkmf edah dtvq