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
What is the best way to show data in a table in Tkinter?
When building Tkinter applications, displaying data in tables is a common requirement. Tkinter provides the Treeview widget from the ttk module, which is the best way to create professional-looking tables with columns, headers, and structured data.
The Treeview widget can display hierarchical data (like file systems) or flat tabular data (like spreadsheets). For table display, we use it in "headings" mode to show column headers without the tree structure.
Basic Table Implementation
Here's how to create a student data table using Treeview ?
import tkinter as tk
from tkinter import ttk
# Create main window
win = tk.Tk()
win.title("Student Data Table")
win.geometry("600x300")
# Create Treeview widget
tree = ttk.Treeview(win, columns=("fname", "lname", "roll"), show='headings', height=8)
# Define column headings
tree.heading("fname", text="First Name")
tree.heading("lname", text="Last Name")
tree.heading("roll", text="Roll Number")
# Configure column widths and alignment
tree.column("fname", width=150, anchor=tk.CENTER)
tree.column("lname", width=150, anchor=tk.CENTER)
tree.column("roll", width=100, anchor=tk.CENTER)
# Insert sample data
students = [
("Amit", "Kumar", "17701"),
("Ankush", "Mathur", "17702"),
("Manisha", "Joshi", "17703"),
("Shivam", "Mehrotra", "17704"),
("Priya", "Singh", "17705")
]
for student in students:
tree.insert("", tk.END, values=student)
# Pack the treeview
tree.pack(pady=20, padx=20, fill=tk.BOTH, expand=True)
win.mainloop()
Enhanced Table with Styling
You can customize the appearance using ttk.Style for better visual appeal ?
import tkinter as tk
from tkinter import ttk
win = tk.Tk()
win.title("Enhanced Student Table")
win.geometry("700x400")
# Configure style
style = ttk.Style()
style.theme_use('clam')
# Configure treeview colors
style.configure("Treeview.Heading",
background="lightblue",
foreground="black",
font=('Arial', 10, 'bold'))
style.configure("Treeview",
background="white",
foreground="black",
rowheight=25,
fieldbackground="white")
# Create Treeview
tree = ttk.Treeview(win, columns=("fname", "lname", "roll", "grade"), show='headings', height=10)
# Define headings
headings = ["First Name", "Last Name", "Roll Number", "Grade"]
columns = ["fname", "lname", "roll", "grade"]
for col, heading in zip(columns, headings):
tree.heading(col, text=heading)
tree.column(col, width=150, anchor=tk.CENTER)
# Sample student data with grades
student_data = [
("Amit", "Kumar", "17701", "A"),
("Ankush", "Mathur", "17702", "B+"),
("Manisha", "Joshi", "17703", "A-"),
("Shivam", "Mehrotra", "17704", "B"),
("Priya", "Singh", "17705", "A+"),
("Rahul", "Verma", "17706", "B-"),
("Sneha", "Sharma", "17707", "A")
]
for data in student_data:
tree.insert("", tk.END, values=data)
# Add scrollbar
scrollbar = ttk.Scrollbar(win, orient=tk.VERTICAL, command=tree.yview)
tree.configure(yscrollcommand=scrollbar.set)
# Pack elements
tree.pack(side=tk.LEFT, fill=tk.BOTH, expand=True, padx=(20, 0), pady=20)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y, pady=20, padx=(0, 20))
win.mainloop()
Key Features
| Feature | Purpose | Example |
|---|---|---|
show='headings' |
Display only column headers | Flat table without tree structure |
columns parameter |
Define column identifiers | columns=("col1", "col2") |
tree.heading() |
Set column headers | tree.heading("col1", text="Name") |
tree.column() |
Configure column properties | Width, alignment, minimum width |
Interactive Table with Selection
Add functionality to handle row selection and display selected data ?
import tkinter as tk
from tkinter import ttk, messagebox
def on_select(event):
selection = tree.selection()[0]
values = tree.item(selection, 'values')
messagebox.showinfo("Selection", f"Selected: {values[0]} {values[1]}")
win = tk.Tk()
win.title("Interactive Student Table")
win.geometry("600x350")
# Create Treeview
tree = ttk.Treeview(win, columns=("fname", "lname", "roll"), show='headings', height=8)
# Configure columns
tree.heading("fname", text="First Name")
tree.heading("lname", text="Last Name")
tree.heading("roll", text="Roll Number")
for col in ("fname", "lname", "roll"):
tree.column(col, width=180, anchor=tk.CENTER)
# Add data
students = [
("John", "Doe", "2001"),
("Jane", "Smith", "2002"),
("Bob", "Johnson", "2003")
]
for student in students:
tree.insert("", tk.END, values=student)
# Bind selection event
tree.bind('<<TreeviewSelect>>', on_select)
tree.pack(pady=20, padx=20, fill=tk.BOTH, expand=True)
win.mainloop()
Conclusion
The ttk.Treeview widget is the best solution for displaying tabular data in Tkinter. It provides professional styling, column management, scrolling support, and interactive features. Use show='headings' for flat tables and customize appearance with ttk.Style for enhanced visual appeal.
