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 display a Listbox with columns using Tkinter?
The Tkinter Treeview widget is the standard way to create a listbox with columns in Python GUI applications. Unlike a basic Listbox, Treeview can display data in tabular format with multiple columns, headers, and sorting capabilities.
Creating a Treeview Widget
The Treeview widget is constructed using ttk.Treeview(parent, columns, **options). The show='headings' parameter hides the tree structure and displays only the columns ?
import tkinter as tk
from tkinter import ttk
# Create main window
window = tk.Tk()
window.title("Listbox with Columns")
window.geometry("700x350")
# Create Treeview widget with 3 columns
tree = ttk.Treeview(window, columns=("c1", "c2", "c3"), show='headings', height=5)
# Define column headings and alignment
tree.column("c1", anchor=tk.CENTER, width=100)
tree.heading("c1", text="ID")
tree.column("c2", anchor=tk.CENTER, width=150)
tree.heading("c2", text="First Name")
tree.column("c3", anchor=tk.CENTER, width=150)
tree.heading("c3", text="Last Name")
# Insert sample data
employees = [
(1, "Joe", "Nash"),
(2, "Emily", "Mackmohan"),
(3, "Estilla", "Roffe"),
(4, "Percy", "Andrews"),
(5, "Stephan", "Heyward")
]
for employee in employees:
tree.insert('', 'end', values=employee)
tree.pack(pady=20)
window.mainloop()
Key Parameters
| Parameter | Description | Example |
|---|---|---|
columns |
Tuple of column identifiers | ("c1", "c2", "c3") |
show |
What to display ('tree', 'headings', or both) | 'headings' |
height |
Number of visible rows | 5 |
Adding Scrollbars
For large datasets, you can add vertical and horizontal scrollbars ?
import tkinter as tk
from tkinter import ttk
window = tk.Tk()
window.title("Scrollable Listbox with Columns")
window.geometry("700x350")
# Create frame for treeview and scrollbars
frame = tk.Frame(window)
frame.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
# Create Treeview
tree = ttk.Treeview(frame, columns=("c1", "c2", "c3"), show='headings', height=8)
# Create scrollbars
v_scrollbar = ttk.Scrollbar(frame, orient=tk.VERTICAL, command=tree.yview)
h_scrollbar = ttk.Scrollbar(frame, orient=tk.HORIZONTAL, command=tree.xview)
tree.configure(yscrollcommand=v_scrollbar.set, xscrollcommand=h_scrollbar.set)
# Configure columns
tree.column("c1", anchor=tk.CENTER, width=80)
tree.heading("c1", text="ID")
tree.column("c2", anchor=tk.CENTER, width=120)
tree.heading("c2", text="Name")
tree.column("c3", anchor=tk.CENTER, width=100)
tree.heading("c3", text="Department")
# Add more data to demonstrate scrolling
departments = ["IT", "HR", "Finance", "Marketing", "Sales"]
for i in range(20):
tree.insert('', 'end', values=(i+1, f"Employee {i+1}", departments[i % 5]))
# Grid layout
tree.grid(row=0, column=0, sticky='nsew')
v_scrollbar.grid(row=0, column=1, sticky='ns')
h_scrollbar.grid(row=1, column=0, sticky='ew')
frame.columnconfigure(0, weight=1)
frame.rowconfigure(0, weight=1)
window.mainloop()
Conclusion
Use Tkinter's Treeview widget to create professional listboxes with columns, headers, and sorting. Add scrollbars for better navigation with large datasets. The Treeview provides much more functionality than basic Listbox widgets.
