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 add a column to a Tkinter TreeView widget?
Tkinter TreeView widget is used to present data in a hierarchical manner in the form of rows and columns. To create a Treeview widget, you have to first create a constructor of Treeview(master, column, show='headings') widget. Here, you can specify the list of columns and pass the value to the column parameter that you want to include in the table.
The indexing of data in the Treeview widget starts from 0. Therefore, to avoid counting the first column, we need to use the show=heading parameter. Let us create an application to show a table with two columns "ID" and "Company" of car manufacturers.
Creating a Basic TreeView
First, let's create a basic TreeView with two columns ?
# 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("700x300")
# Create an instance of Style widget
style = ttk.Style()
style.theme_use('clam')
# Add a Treeview widget
tree = ttk.Treeview(win, column=("c1", "c2"), show='headings', height=8)
tree.column("# 1", anchor=CENTER)
tree.heading("# 1", text="ID")
tree.column("# 2", anchor=CENTER)
tree.heading("# 2", text="Company")
# Insert the data in Treeview widget
tree.insert('', 'end', text="1", values=('1', 'Honda'))
tree.insert('', 'end', text="2", values=('2', 'Hyundai'))
tree.insert('', 'end', text="3", values=('3', 'Tesla'))
tree.insert('', 'end', text="4", values=('4', 'Volkswagen'))
tree.insert('', 'end', text="5", values=('5', 'Tata Motors'))
tree.insert('', 'end', text="6", values=('6', 'Renault'))
tree.pack()
win.mainloop()
Adding a New Column to Existing TreeView
To add a new column to an existing TreeView, you need to redefine the columns and configure them properly ?
from tkinter import *
from tkinter import ttk
# Create an instance of tkinter frame
win = Tk()
win.geometry("800x300")
# Create TreeView with initial columns
tree = ttk.Treeview(win, show='headings', height=8)
# Define columns - including the new one
tree["columns"] = ("c1", "c2", "c3")
# Configure each column
tree.column("c1", anchor=CENTER, width=100)
tree.heading("c1", text="ID")
tree.column("c2", anchor=CENTER, width=150)
tree.heading("c2", text="Company")
# New column added
tree.column("c3", anchor=CENTER, width=120)
tree.heading("c3", text="Country")
# Insert data with the new column
tree.insert('', 'end', values=('1', 'Honda', 'Japan'))
tree.insert('', 'end', values=('2', 'Hyundai', 'South Korea'))
tree.insert('', 'end', values=('3', 'Tesla', 'USA'))
tree.insert('', 'end', values=('4', 'Volkswagen', 'Germany'))
tree.insert('', 'end', values=('5', 'Tata Motors', 'India'))
tree.pack(pady=20)
win.mainloop()
Dynamically Adding Columns
You can also add columns dynamically using a function ?
from tkinter import *
from tkinter import ttk
def add_column():
# Get current columns
current_cols = list(tree["columns"])
# Add new column
new_col = f"c{len(current_cols) + 1}"
current_cols.append(new_col)
# Update TreeView columns
tree["columns"] = current_cols
# Configure the new column
tree.column(new_col, anchor=CENTER, width=100)
tree.heading(new_col, text=f"Column {len(current_cols)}")
print(f"Added column: {new_col}")
# Create main window
win = Tk()
win.geometry("600x400")
# Create TreeView
tree = ttk.Treeview(win, columns=("c1", "c2"), show='headings', height=6)
# Configure initial columns
tree.column("c1", anchor=CENTER, width=100)
tree.heading("c1", text="ID")
tree.column("c2", anchor=CENTER, width=150)
tree.heading("c2", text="Name")
# Add some initial data
tree.insert('', 'end', values=('1', 'Item A'))
tree.insert('', 'end', values=('2', 'Item B'))
# Button to add new column
btn_add = Button(win, text="Add Column", command=add_column)
tree.pack(pady=10)
btn_add.pack(pady=5)
win.mainloop()
Key Points
- Use the
columnsparameter to define column identifiers - Configure each column with
tree.column()andtree.heading() - When adding columns dynamically, update the
tree["columns"]property - Remember to update existing data when adding new columns
Conclusion
Adding columns to a Tkinter TreeView requires defining them in the columns parameter and configuring each with proper headings and properties. For dynamic column addition, update the columns property and configure the new column appropriately.
