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.