Recently in a Python webinar the topic of discussion was how to use the Tkinter Treeview widget in Python, many questions and doubts were raised during the webinar. In this tutorial, I will explain all the functionalities step by step and help you to understand with examples.
Use the Tkinter Treeview Widget in Python
Let us learn how to use the Tkinter Treeview widget in Python.
Read How to Create a Text Box in Python Tkinter?
1. Create a Basic Treeview
The Treeview widget is a way to display tabular data in a Tkinter application. Here, we initialize the main window, set its title, and define the structure of our Treeview table.
import tkinter as tk
from tkinter import ttk, messagebox
# Create the main window
root = tk.Tk()
root.title("Customer Database")
root.geometry("500x400")
# Create a Treeview widget
tree = ttk.Treeview(root)
tree["columns"] = ("name", "email", "phone")
tree.column("#0", width=0, stretch=tk.NO) # Hide default column
tree.column("name", width=150)
tree.column("email", width=200)
tree.column("phone", width=150)
tree.heading("name", text="Name")
tree.heading("email", text="Email")
tree.heading("phone", text="Phone Number")
tree.pack(expand=True, fill=tk.BOTH)The Treeview widget has three columns: Name, Email, and Phone. The first column (#0) is hidden to make the table visually clean. tree.pack(expand=True, fill=tk.BOTH) ensures the table resizes dynamically.
Check out How to Create Labels in Python with Tkinter?
2. Add Search Functionality
This section allows the user to search for a specific entry in the table and highlight it.
# Function to search in Treeview
def search_treeview(query):
items = tree.get_children()
for item in items:
if query.lower() in str(tree.item(item)['values']).lower():
tree.selection_set(item)
tree.focus(item)
return
messagebox.showinfo("Search", f"No results found for '{query}'.")
# Search Entry
search_entry = ttk.Entry(root)
search_entry.pack(side=tk.TOP, padx=10, pady=5)
# Search Button
search_button = ttk.Button(root, text="Search", command=lambda: search_treeview(search_entry.get()))
search_button.pack(side=tk.TOP, padx=10, pady=5)search_entry: An input field where users type their search query. search_button: A button that triggers the search function.
Read How to Create Buttons in Python with Tkinter?
3. Populate the Treeview with Real Data
We fetch customer data from a CSV file and display it in the Treeview.
import csv
import os
# Function to load data from CSV file
def load_data():
if not os.path.exists("customers.csv"):
messagebox.showerror("Error", "customers.csv file not found!")
return
with open("customers.csv", "r") as file:
reader = csv.DictReader(file)
for row in reader:
tree.insert("", tk.END, values=(row["Name"], row["Email"], row["Phone"]))
# Load data into Treeview
load_data()Checks if customers.csv exists: If not, it alerts the user. Reads the CSV file using csv.DictReader: This allows us to fetch data using column names (Name, Email, Phone). Inserts each row into the Treeview: The data is displayed dynamically.
Read How to Create a Menu Bar in Tkinter?
4. Create the CSV File (customers.csv)
Before running the script, ensure you have a CSV file named customers.csv with the following content:
Name,Email,Phone
John Smith,johnsmith@example.com,(123) 456-7890
Jane Doe,janedoe@example.com,(987) 654-3210
Michael Johnson,michaelj@example.com,(555) 123-4567
Emily Brown,emilybrown@example.com,(222) 987-6543Check out How to Use Tkinter Entry Widget in Python?
5. Run the Application
Finally, we start the Tkinter main loop to keep the GUI running.
root.mainloop()You can see the output in the screenshot below.

Read How to Create Checkboxes in Python Tkinter?
Conclusion
In this tutorial, I helped you learn how to use the Tkinter Treeview widget in Python. I explained step by step the process of creating a basic treeview, adding search functionality, populating the treeview with real data, creating the CSV file, and on running the application we get accurate output.
You may read:
- How to Create and Customize Listboxes in Python Tkinter?
- How to Create Radio Buttons in Python with Tkinter?
- How to use Tkinter Filedialog in Python

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.