Tkinter destroy() method | Python

In this article, you will know about Tkinter destroy() function in Python programming.

The Tkinter is a predefined module that is defined by Python. The module is mainly used to create graphical pages and to perform various operations on the pages. This module can be accessed by importing it which can be done as shown:

import tkinter

The Tkinter module performs many operations. The destroy() method from Tkinter is used to destroy a widget. This method is also useful to control the behavior of various widgets based on the other widgets. It is also used to destroy the GUI components after the completion of work to free up space.

The syntax for the Tkinter destroy() method is:

widget_object = Widget(parent, command = widget_class_object.destroy)

An example of this method is:

from tkinter import *
from tkinter.ttk import *
#tkinter window
base = Tk()
#This button can close the window
but_1 = Button(base, text ="I close the Window", command = base.destroy)
#Exteral paddign for our buttons
but_1.pack(pady = 40)
#This button for closing our first button
but_2 = Button(base, text ="I close the first button", command = but_1.destroy)
but_2.pack(pady = 40)
#This button close the second button
but_3 = Button(base, text ="I close the second button", command = but_2.destroy)
but_3.pack(pady = 40)


We will see the result given in the below UI screenshot:

Tkinter destroy() method

In the above, we create three buttons that are used to destroy the other widgets and buttons. The first button is used to destroy the window, second button is used to destroy the first button.

Leave a Reply

Your email address will not be published. Required fields are marked *