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
Which widget do you use for an Excel-like table in Tkinter?
Tkinter is a standard Python library used to build GUI-based desktop applications. For creating Excel-like tables in Tkinter, the Treeview widget is the go-to solution.
The Treeview widget displays data in a tabular format similar to MS Excel. You can define columns, insert rows of data, and even integrate with libraries like Pandas or NumPy for data manipulation.
Basic Syntax
The Treeview widget is created using the following syntax ?
ttk.Treeview(parent, columns=(col1, col2, ...), show='headings', **options)
Creating a Simple Table
Here's how to create a basic Excel-like table with columns and data ?
# Import the required libraries
from tkinter import *
from tkinter import ttk
# Create an instance of tkinter frame
win = Tk()
# Set the size of the tkinter window
win.geometry("700x350")
win.title("Excel-like Table")
# Add a Treeview widget
tree = ttk.Treeview(win, columns=("c1", "c2", "c3"), show='headings')
# Define column headings and properties
tree.column("c1", anchor=CENTER, width=100)
tree.heading("c1", text="ID")
tree.column("c2", anchor=CENTER, width=150)
tree.heading("c2", text="First Name")
tree.column("c3", anchor=CENTER, width=150)
tree.heading("c3", text="Last Name")
# Insert sample data
tree.insert('', 'end', values=('001', 'John', 'Doe'))
tree.insert('', 'end', values=('002', 'Jane', 'Smith'))
tree.insert('', 'end', values=('003', 'Bob', 'Johnson'))
tree.pack(pady=20)
win.mainloop()
Adding Multiple Rows
You can add multiple rows using a loop for better organization ?
from tkinter import *
from tkinter import ttk
win = Tk()
win.geometry("700x400")
win.title("Employee Table")
# Create Treeview
tree = ttk.Treeview(win, columns=("ID", "Name", "Department", "Salary"), show='headings')
# Define headings
tree.heading("ID", text="Employee ID")
tree.heading("Name", text="Name")
tree.heading("Department", text="Department")
tree.heading("Salary", text="Salary")
# Set column widths
tree.column("ID", width=100)
tree.column("Name", width=150)
tree.column("Department", width=150)
tree.column("Salary", width=100)
# Sample employee data
employees = [
("E001", "Alice Brown", "Engineering", "$75000"),
("E002", "Charlie Davis", "Marketing", "$65000"),
("E003", "Diana Wilson", "HR", "$60000"),
("E004", "Frank Miller", "Finance", "$70000")
]
# Insert data using loop
for employee in employees:
tree.insert('', 'end', values=employee)
tree.pack(pady=20)
win.mainloop()
Key Features
| Feature | Description | Usage |
|---|---|---|
columns |
Define column identifiers | columns=("c1", "c2") |
show='headings' |
Display only column headers | Hides tree structure |
anchor |
Text alignment in columns | CENTER, W, E |
width |
Set column width | width=150 |
Conclusion
The Treeview widget is the best choice for creating Excel-like tables in Tkinter. It provides column management, data insertion, and professional table formatting capabilities for GUI applications.
