- 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 all children's elements using Python's Tkinter?
Frames are very useful in a Tkinter application. If we define a Frame in an application, it means we have the privilege to add a group of widgets inside it. However, all these widgets are called Children of that particular Frame.
Let us suppose that we want to remove all the children widgets defined in a frame. Then, first we have to get the focus on children using the winfo_children() method. Once we get the focus, we can delete all the existing children using destroy() method.
#Import the Tkinter Library from tkinter import * #Create an instance of Tkinter Frame win = Tk() #Set the geometry of window win.geometry("700x350") #Initialize a Frame frame = Frame(win) def clear_all(): for item in frame.winfo_children(): item.destroy() button.config(state= "disabled") #Define a ListBox widget listbox = Listbox(frame, height=10, width= 15, bg= 'grey', activestyle= 'dotbox',font='aerial') listbox.insert(1,"Go") listbox.insert(1,"Java") listbox.insert(1,"Python") listbox.insert(1,"C++") listbox.insert(1,"Ruby") listbox.pack() label = Label(win, text= "Top 5 Programming Languages", font= ('Helvetica 15 bold')) label.pack(pady= 20) frame.pack() #Create a button to remove all the children in the frame button = Button(win, text= "Clear All", font= ('Helvetica 11'), command= clear_all) button.pack() win.mainloop()
If we execute the above code, it will display a window with a list of items in a list box and a button.
When we click the "Clear All" button, it will remove all the children lying inside the frame object.
- Related Articles
- How to Delete Tkinter Text Box's Contents?
- How can I get all element's immediate children with css selectors using selenium?
- Latest Children's Fashion Trends
- How to delete lines from a Python tkinter canvas?
- How to set a widget's size in Tkinter?
- Python program to get all subsets having sum s\n
- Segregate 0’s and 1’s in an array list using Python?
- How to delete all elements from arraylist for listview in Android?
- How to change a Tkinter widget's font style without knowing the widget's font family/size?
- Minimum Swaps to Group All 1's Together in Python
- What's the difference between Tkinter's Tk and Toplevel classes?
- How can I pass arguments to Tkinter button's callback command?
- How to delete all the file contents using PowerShell?
- Reduce array's dimension by adding all the elements in Numpy