Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to clear an entire Treeview with Tkinter?
Tkinter Treeview widgets are used to display hierarchical data in a tree-like structure, similar to file explorers in Windows or Mac OS. When working with dynamic content, you often need to clear all items from a Treeview widget.
To clear an entire Treeview, you can use the delete() method while iterating through all child items using get_children().
Syntax
for item in treeview.get_children():
treeview.delete(item)
Example − Creating and Clearing a Treeview
In this example, we will create a treeview with programming languages categorized by Frontend and Backend, then demonstrate how to clear all items ?
# Import the required library
from tkinter import *
from tkinter import ttk
# Create an instance of tkinter frame
win = Tk()
win.title("Programming Languages Treeview")
win.geometry("600x300")
# Create a label
ttk.Label(win, text="Programming Languages Hierarchy").pack(pady=10)
# Treeview List Instantiation
treeview = ttk.Treeview(win)
treeview.pack(pady=10)
# Insert root and category items
treeview.insert('', '0', 'language', text='Programming Languages')
treeview.insert('', '1', 'frontend', text='Frontend')
treeview.insert('', '2', 'backend', text='Backend')
# Insert frontend languages
treeview.insert('frontend', 'end', text='JavaScript')
treeview.insert('frontend', 'end', text='TypeScript')
treeview.insert('frontend', 'end', text='React')
# Insert backend languages
treeview.insert('backend', 'end', text='Python')
treeview.insert('backend', 'end', text='Java')
treeview.insert('backend', 'end', text='Node.js')
# Move categories under main language node
treeview.move('frontend', 'language', 'end')
treeview.move('backend', 'language', 'end')
# Function to clear treeview
def clear_treeview():
for item in treeview.get_children():
treeview.delete(item)
# Add clear button
clear_btn = ttk.Button(win, text="Clear Treeview", command=clear_treeview)
clear_btn.pack(pady=10)
win.mainloop()
How It Works
The clearing process works in two steps:
-
get_children()returns a tuple of all top-level items in the treeview -
delete(item)removes each item and all its descendants
Alternative Method − Using delete with Empty String
You can also clear all items at once by passing an empty string to get all children ?
# Alternative method to clear treeview treeview.delete(*treeview.get_children())
This method uses the unpacking operator (*) to pass all children as separate arguments to the delete method.
Complete Working Example
from tkinter import *
from tkinter import ttk
def create_sample_treeview():
# Clear existing items first
for item in tree.get_children():
tree.delete(item)
# Add sample data
tree.insert('', 'end', text='Languages', values=('Category',))
frontend = tree.insert('', 'end', text='Frontend', values=('Web Development',))
backend = tree.insert('', 'end', text='Backend', values=('Server Side',))
tree.insert(frontend, 'end', text='JavaScript', values=('Dynamic',))
tree.insert(frontend, 'end', text='HTML/CSS', values=('Markup',))
tree.insert(backend, 'end', text='Python', values=('Versatile',))
tree.insert(backend, 'end', text='Java', values=('Enterprise',))
def clear_all():
for item in tree.get_children():
tree.delete(item)
# Create main window
root = Tk()
root.title("Treeview Clear Demo")
root.geometry("500x300")
# Create treeview with columns
tree = ttk.Treeview(root, columns=('Description',), show='tree headings')
tree.heading('#0', text='Technology')
tree.heading('Description', text='Description')
tree.pack(fill='both', expand=True, padx=10, pady=10)
# Create buttons
btn_frame = ttk.Frame(root)
btn_frame.pack(pady=10)
ttk.Button(btn_frame, text="Load Sample Data", command=create_sample_treeview).pack(side='left', padx=5)
ttk.Button(btn_frame, text="Clear All", command=clear_all).pack(side='left', padx=5)
root.mainloop()
Key Points
-
get_children()returns only top-level items; deleting them removes all descendants - The delete operation is immediate and cannot be undone
- Always iterate through a copy if you need to selectively delete items
- Use the unpacking method for a more concise approach
Conclusion
Clearing a Tkinter Treeview is straightforward using get_children() and delete(). This method efficiently removes all items and their hierarchical structure in one operation.
