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.

Syntax

widget.winfo_children()    # Returns list of child widgets
child.destroy()           # Destroys the widget

Method 1: Using winfo_children() and destroy()

This is the most common approach to delete all children widgets ?

import tkinter as tk

# Create main window
root = tk.Tk()
root.geometry("400x300")
root.title("Delete Children Demo")

# Create a frame to hold child widgets
frame = tk.Frame(root, bg="lightgray", padx=10, pady=10)
frame.pack(pady=20)

# Add some child widgets to the frame
label1 = tk.Label(frame, text="Label 1", bg="yellow")
label1.pack(pady=5)

label2 = tk.Label(frame, text="Label 2", bg="lightblue")
label2.pack(pady=5)

entry = tk.Entry(frame)
entry.pack(pady=5)

def delete_all_children():
    for child in frame.winfo_children():
        child.destroy()
    status_label.config(text="All children deleted!")

# Button to delete children
delete_btn = tk.Button(root, text="Delete All Children", command=delete_all_children)
delete_btn.pack(pady=10)

# Status label
status_label = tk.Label(root, text="Children present in frame")
status_label.pack()

root.mainloop()

Method 2: Complete Example with Listbox

Here's a more comprehensive example showing how to delete children from a frame containing a listbox ?

import tkinter as tk

# Create an instance of Tkinter window
win = tk.Tk()
win.geometry("500x400")
win.title("Clear Frame Children")

# Initialize a Frame
frame = tk.Frame(win, bg="lightgray", padx=20, pady=20)

def clear_all():
    for item in frame.winfo_children():
        item.destroy()
    button.config(state="disabled")
    status_label.config(text="All items cleared!")

# Define a ListBox widget
listbox = tk.Listbox(frame, height=8, width=20, bg='white', 
                    activestyle='dotbox', font=('Arial', 10))
listbox.insert(0, "Python")
listbox.insert(1, "Java")
listbox.insert(2, "C++")
listbox.insert(3, "JavaScript")
listbox.insert(4, "Go")
listbox.pack(pady=10)

# Add another child widget
info_label = tk.Label(frame, text="Programming Languages", 
                     font=('Arial', 12, 'bold'), bg="lightgray")
info_label.pack(pady=5)

# Pack the frame
label = tk.Label(win, text="Top Programming Languages", 
                font=('Helvetica', 15, 'bold'))
label.pack(pady=20)
frame.pack()

# Create a button to remove all the children in the frame
button = tk.Button(win, text="Clear All", font=('Helvetica', 11), 
                  command=clear_all, bg="red", fg="white")
button.pack(pady=10)

# Status label
status_label = tk.Label(win, text="Items are loaded", fg="blue")
status_label.pack()

win.mainloop()

How It Works

The process involves these key steps:

  1. frame.winfo_children() returns a list of all child widgets
  2. Loop through each child widget in the list
  3. Call destroy() method on each child to remove it
  4. Optionally update the UI to reflect the changes

Key Points

  • winfo_children() only returns direct children, not nested grandchildren
  • destroy() permanently removes the widget from memory
  • After destruction, widget references become invalid
  • The frame itself remains intact, only its children are removed

Output

When you run the code, you'll see a window with a listbox containing programming languages. Clicking "Clear All" removes all widgets from the frame, leaving it empty.

Clear Frame Children Top Programming Languages Python Java C++ JavaScript Clear All

Conclusion

Use winfo_children() combined with destroy() to remove all child widgets from a frame. This method is efficient and commonly used for clearing dynamic content in Tkinter applications.

---
Updated on: 2026-03-25T20:44:08+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements