How would I make destroy() method in Tkinter work with my code?

TkinterServer Side ProgrammingProgramming

In order to close or remove any widget in an existing Tkinter application, we can use the destroy() method. It terminates the widget process abruptly within the program. The method can be invoked with the specific widget we want to close.

Example

In this example, we will create a button to remove the Label Widget from the application.

#Import required libraries
from tkinter import *
from tkinter import ttk
#Create an instance of Tkinter frame
win= Tk()

#Define the geometry of the window
win.geometry("750x250")

#Define a function to destroy the label widget
def close_widget():
   label.destroy()

#Create a label
label= Label(win, text= "Hey There! How are you?", font= ('Helvetica20 italic'))
label.pack(pady=30)

#Create a Button to remove the Label
ttk.Button(win, text="Remove", command= close_widget).pack(pady=20)
win.mainloop()

Output

Running the above code will display a window that contains a label widget and a button in it.

Now, click the Remove button to destroy the label widget in the frame.

raja
Published on 03-May-2021 11:31:25
Advertisements