How to attach a vertical scrollbar to a Treeview using Tkinter?

The Treeview widget in Tkinter displays hierarchical data in a table format with columns. When displaying large datasets, a vertical scrollbar becomes essential for navigation. This tutorial shows how to attach a vertical scrollbar to a Treeview widget.

Basic Treeview with Scrollbar

To add a scrollbar, create a Scrollbar widget and link it to the Treeview using yscrollcommand and command properties ?

import tkinter as tk
from tkinter import ttk

# Create main window
root = tk.Tk()
root.title("Treeview with Scrollbar")
root.geometry("500x300")

# Create Treeview widget
tree = ttk.Treeview(root, columns=("Name", "Age"), show="headings", height=6)
tree.heading("#1", text="Name")
tree.heading("#2", text="Age")
tree.column("#1", width=200, anchor="center")
tree.column("#2", width=100, anchor="center")

# Add sample data
data = [
    ("Alice", "25"), ("Bob", "30"), ("Charlie", "35"),
    ("Diana", "28"), ("Eve", "32"), ("Frank", "29"),
    ("Grace", "27"), ("Henry", "31"), ("Ivy", "26"),
    ("Jack", "33"), ("Kelly", "24"), ("Liam", "36")
]

for person in data:
    tree.insert("", "end", values=person)

# Create vertical scrollbar
scrollbar = ttk.Scrollbar(root, orient="vertical", command=tree.yview)
tree.configure(yscrollcommand=scrollbar.set)

# Pack widgets
tree.pack(side="left", fill="both", expand=True)
scrollbar.pack(side="right", fill="y")

root.mainloop()

How It Works

The scrollbar connection works through two key configurations:

  • tree.configure(yscrollcommand=scrollbar.set) ? Updates scrollbar position when Treeview scrolls
  • scrollbar = ttk.Scrollbar(command=tree.yview) ? Makes scrollbar control Treeview's vertical view

Advanced Example with Grid Layout

For better control over widget positioning, use the grid geometry manager ?

import tkinter as tk
from tkinter import ttk

# Create main window
root = tk.Tk()
root.title("Employee Database")
root.geometry("600x400")

# Create frame for treeview and scrollbar
frame = tk.Frame(root)
frame.pack(fill="both", expand=True, padx=10, pady=10)

# Create Treeview
tree = ttk.Treeview(frame, columns=("ID", "Name", "Department", "Salary"), show="headings")

# Configure columns
columns = {
    "ID": ("ID", 50),
    "Name": ("Employee Name", 150),
    "Department": ("Department", 120),
    "Salary": ("Salary ($)", 100)
}

for col, (heading, width) in columns.items():
    tree.heading(col, text=heading)
    tree.column(col, width=width, anchor="center")

# Add employee data
employees = [
    ("001", "John Smith", "Engineering", "75000"),
    ("002", "Jane Doe", "Marketing", "65000"),
    ("003", "Mike Johnson", "Sales", "55000"),
    ("004", "Sarah Wilson", "HR", "60000"),
    ("005", "Tom Brown", "Engineering", "80000"),
    ("006", "Lisa Davis", "Finance", "70000"),
    ("007", "Chris Lee", "Marketing", "62000"),
    ("008", "Amy Chen", "Engineering", "78000"),
    ("009", "David Miller", "Sales", "58000"),
    ("010", "Emma Taylor", "HR", "63000")
]

for employee in employees:
    tree.insert("", "end", values=employee)

# Create scrollbar
scrollbar = ttk.Scrollbar(frame, orient="vertical", command=tree.yview)
tree.configure(yscrollcommand=scrollbar.set)

# Grid layout
tree.grid(row=0, column=0, sticky="nsew")
scrollbar.grid(row=0, column=1, sticky="ns")

# Configure grid weights
frame.grid_rowconfigure(0, weight=1)
frame.grid_columnconfigure(0, weight=1)

root.mainloop()

Key Parameters

Parameter Description Example Value
orient Scrollbar direction "vertical" or "horizontal"
command Function to call when scrolling tree.yview
yscrollcommand Links Treeview to scrollbar scrollbar.set

Conclusion

Adding a vertical scrollbar to a Treeview requires linking the widgets with yscrollcommand and command parameters. Use pack() for simple layouts or grid() for more complex positioning control.

Updated on: 2026-03-26T18:49:23+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements