Saturday, November 16, 2024
Google search engine
HomeLanguagesHow to Close a Tkinter Window With a Button?

How to Close a Tkinter Window With a Button?

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




# Python program to create a close button
# using destroy Non-Class method
from tkinter import *
  
# Creating the tkinter window
root = Tk()
root.geometry("200x100")
  
# Button for closing
exit_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 method
from tkinter import *
  
# Creating the tkinter window
root = Tk()
root.geometry("200x100")
  
# Function for closing window
  
  
def Close():
    root.destroy()
  
  
# Button for closing
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




# Python program to create a close button
# using destroy Class method
from 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 window
test = Window()


Example: Using destroy() in a function

Python3




# Python program to create a close button
# using destroy Class method
from 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 window
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




# Python program to create a close button
# using quit method
from tkinter import *
  
# Creating the tkinter window
root = Tk()
root.geometry("200x100")
  
# Button for closing
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

RELATED ARTICLES

Most Popular

Recent Comments