- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- 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 to delete Tkinter widgets from a window?
Sometimes, we want to remove a widget that is of no use in the application. We can delete widgets from the window or frame using the .destroy method in tkinter. It can be invoked in the widget by defining a function for it.
In this example, we have created a button that will remove the text label widget from the window.
#Import the tkinter library from tkinter import * #Create an instance of tkinter frame win = Tk() #Set the geometry win.geometry("650x450") #Define a function to remove the text from the screen def delete_text(): text.destroy() #Create a text widget text= Label(win,text="This is a New Line", font=('Aerial bold', 20)) text.pack(pady=20) #Create a button for Deleting Widget Button(win, text= "Click Here", font=('bold',20), command= delete_text).pack(pady=10) win.mainloop()
Running the above code will produce the following output −
Now, click the "Click Here" button. It will delete the Label Text widget from the window.
- Related Articles
- How to set padding of all widgets inside a window or frame in Tkinter?
- How to create transparent widgets using Tkinter?
- How to delete lines from a Python tkinter canvas?
- How to show and hide widgets in Tkinter?
- How to capture events on Tkinter child widgets?
- How to copy from clipboard using tkinter without displaying a window
- Adding a scrollbar to a group of widgets in Tkinter
- How to set the border color of certain Tkinter widgets?
- How to add a margin to a tkinter window?
- How to make a Tkinter window not resizable?
- Showing and Hiding widgets in Tkinter?
- How can I prevent a window from being resized with Tkinter?
- How to add space between two widgets placed in a grid in tkinter?
- How do you overlap widgets/frames in Python tkinter?
- How to set a Tkinter window to a constant size?