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 change ttk.Treeview column width and weight in Python 3.3?
To display large datasets in a Tkinter application, we can use the ttk.Treeview widget. The Treeview widget allows us to represent data in a tabular format with customizable columns and rows.
To configure the column width and behavior of the Treeview widget, we use the column() method with width and stretch properties. These properties control how columns are sized and whether they resize with the window.
Column Configuration Parameters
The main parameters for column configuration are −
- width − Sets the initial column width in pixels
- stretch − Controls whether the column expands to fill available space (YES/NO)
- anchor − Aligns content within the column (CENTER, W, E, etc.)
Example
In this example, we create a table displaying programming languages with customized column widths. The 'ID' column has a fixed width of 100 pixels, while the 'Programming Language' column adapts to content ?
# 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("Treeview Column Configuration")
# 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)
# Configure column widths and properties
tree.column("# 1", anchor=CENTER, stretch=NO, width=100)
tree.heading("# 1", text="ID")
tree.column("# 2", anchor=CENTER, stretch=NO, width=200)
tree.heading("# 2", text="Programming Language")
# Insert the data in Treeview widget
tree.insert('', 'end', values=('1', 'C++'))
tree.insert('', 'end', values=('2', 'Java'))
tree.insert('', 'end', values=('3', 'Python'))
tree.insert('', 'end', values=('4', 'Golang'))
tree.insert('', 'end', values=('5', 'JavaScript'))
tree.insert('', 'end', values=('6', 'C#'))
tree.insert('', 'end', values=('7', 'Rust'))
tree.insert('', 'end', values=('8', 'SQL'))
tree.pack(pady=20)
win.mainloop()
Output
The output displays a table with programming languages where the ID column has a fixed width of 100 pixels and the Programming Language column has a width of 200 pixels ?
Different Width Configurations
Here's an example showing different approaches to column width management ?
from tkinter import *
from tkinter import ttk
win = Tk()
win.geometry("800x400")
tree = ttk.Treeview(win, columns=("col1", "col2", "col3"), show='headings')
# Different width configurations
tree.column("col1", width=80, stretch=NO) # Fixed width, no stretching
tree.column("col2", width=150, stretch=YES) # Initial width, can stretch
tree.column("col3", width=100, stretch=NO) # Fixed width
tree.heading("col1", text="ID")
tree.heading("col2", text="Description")
tree.heading("col3", text="Status")
# Sample data
data = [
("1", "Learn Python Programming", "Active"),
("2", "Master Data Structures", "Pending"),
("3", "Build GUI Applications", "Complete")
]
for item in data:
tree.insert('', 'end', values=item)
tree.pack(fill=BOTH, expand=True, padx=10, pady=10)
win.mainloop()
Conclusion
Use the column() method to control Treeview column widths with width and stretch parameters. Set stretch=NO for fixed-width columns and stretch=YES for resizable columns that adapt to window size changes.
