Tkinter frames are used to group and organize too many widgets in an aesthetic way. A frame component can contain Button widgets, Entry Widgets, Labels, ScrollBars, and other widgets.
If we want to clear the frame content or delete all the widgets inside the frame, we can use the destroy() method. This method can be invoked by targeting the children of the frame using winfo_children().
Example
#Import the required libraries from tkinter import * #Create an instance of tkinter frame win= Tk() #Set the geometry of frame win.geometry("600x250") #Create a frame frame = Frame(win) frame.pack(side="top", expand=True, fill="both") #Create a text label Label(frame,text="Enter the Password", font=('Helvetica',20)).pack(pady=20) def clear_frame(): for widgets in frame.winfo_children(): widgets.destroy() #Create a button to close the window Button(frame, text="Clear", font=('Helvetica bold', 10), command= clear_frame).pack(pady=20) win.mainloop()
Output
Running the above code will display a window containing a button “Clear” that targets all the widgets inside the frame and clears it.
Now click on the “Clear” Button and it will clear all the widgets inside the frame.