- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How would I make destroy() method in Tkinter work with my code?
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.
Advertisements