How to Close a Tkinter Window With a Button? - GeeksforGeeks

17 Dec, 2020
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. The task can be assigned in the command parameter of Button() function. Given below are various methods by which this can be achieved.
Approach:
Import tkinter module.
Create a main window named root.
Add a button.
Assign root.destroy to the command attribute of that button.
Example: Using destroy() directly in command attribute
Python3
# Python program to create a close button# using destroy Non-Class methodfrom tkinter import *  # Creating the tkinter windowroot = Tk()root.geometry("200x100")  # Button for closingexit_button = Button(root, text="Exit", command=root.destroy)exit_button.pack(pady=20)  root.mainloop()
Example: Using destroy() in a function
Python3
# Python program to create a close button# using destroy Non-Class methodfrom tkinter import *  # Creating the tkinter windowroot = Tk()root.geometry("200x100")  # Function for closing window    def Close():    root.destroy()    # Button for closingexit_button = Button(root, text="Exit", command=Close)exit_button.pack(pady=20)  root.mainloop()
Output:
Approach:
Import tkinter module.
Create a tkinter window class.
Create a main window named root.
Add a button.
Assign root.destroy to the command attribute of that button.
Example: Using destroy() directly in command attribute
Python3
# Python program to create a close button# using destroy Class methodfrom tkinter import *  # Class for tkinter window    class Window():    def __init__(self):          # Creating the tkinter Window        self.root = Tk()        self.root.geometry("200x100")          # Button for closing        exit_button = Button(self.root, text="Exit", command=self.root.destroy)        exit_button.pack(pady=20)          self.root.mainloop()    # Running test windowtest = Window()
Example: Using destroy() in a function
Python3
# Python program to create a close button# using destroy Class methodfrom tkinter import *  # Class for tkinter window    class Window():    def __init__(self):          # Creating the tkinter Window        self.root = Tk()        self.root.geometry("200x100")          # Button for closing        exit_button = Button(self.root, text="Exit", command=self.Close)        exit_button.pack(pady=20)          self.root.mainloop()      # Function for closing window    def Close(self):        self.root.destroy()    # Running test windowtest = Window()
Output:
This method doesn’t work properly if you’re calling your Tkinter app from IDLE as quit() will terminate the whole TCL interpreter and cause the mainloop to exit leaving all the widgets intact. So, it is better to use quit() if you’re using any other editor/interpreter other than IDLE. Or, you can use exit() function after mainloop to exit from the Python program.
It is not recommended to use quit() if your Tkinter application is executed from IDLE as it will close the interpreter leaving the program running with all its widgets. It is also mainly not recommended because it may fail in some interpreters. 
Approach:
Import tkinter module.
Create a main window named root.
Add a button.
Assign root.quit to the command attribute of that button.
Add exit() function after calling the mainloop
Example:
Python3
# Python program to create a close button# using quit methodfrom tkinter import *  # Creating the tkinter windowroot = Tk()root.geometry("200x100")  # Button for closingexit_button = Button(root, text="Exit", command=root.quit)exit_button.pack(pady=20)  root.mainloop()exit(0)
Output:
Output in Normal Editor (VS Code)
Output in IDLE
Output in Jupyter Notebook
Picked
Python-tkinter
Technical Scripter 2020
Python
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Read a file line by line in Python
How to Install PIP on Windows ?
Enumerate() in Python
Different ways to create Pandas Dataframe
Iterate over a list in Python
Python String | replace()
*args and **kwargs in Python
Reading and Writing to text files in Python
Create a Pandas DataFrame from Lists