How to Close a Tkinter Window With a Button?
Last Updated :
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.
Method 1: Using destroy() Non-Class method
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
from tkinter import *
root = Tk()
root.geometry( "200x100" )
exit_button = Button(root, text = "Exit" , command = root.destroy)
exit_button.pack(pady = 20 )
root.mainloop()
|
Example: Using destroy() in a function
Python3
from tkinter import *
root = Tk()
root.geometry( "200x100" )
def Close():
root.destroy()
exit_button = Button(root, text = "Exit" , command = Close)
exit_button.pack(pady = 20 )
root.mainloop()
|
Output:
Method 2: Using destroy() Class method
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
from tkinter import *
class Window():
def __init__( self ):
self .root = Tk()
self .root.geometry( "200x100" )
exit_button = Button( self .root, text = "Exit" , command = self .root.destroy)
exit_button.pack(pady = 20 )
self .root.mainloop()
test = Window()
|
Example: Using destroy() in a function
Python3
from tkinter import *
class Window():
def __init__( self ):
self .root = Tk()
self .root.geometry( "200x100" )
exit_button = Button( self .root, text = "Exit" , command = self .Close)
exit_button.pack(pady = 20 )
self .root.mainloop()
def Close( self ):
self .root.destroy()
test = Window()
|
Output:
Method 3: Using quit() method
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
from tkinter import *
root = Tk()
root.geometry( "200x100" )
exit_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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...