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 can I set the row height in Tkinter TreeView?
The Treeview widget in Tkinter provides a way to display data in a hierarchical table structure. You can customize the appearance of the Treeview, including setting custom row heights using the ttk.Style configuration.
To set the row height in a Tkinter Treeview, you need to use the ttk.Style() class and configure the rowheight property. This property adds internal padding to each row, making them taller or shorter as needed.
Syntax
style = ttk.Style()
style.configure('Treeview', rowheight=height_value)
Example
Here's how to create a Treeview with custom row height ?
# 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")
# Create a style object and set theme
style = ttk.Style()
style.theme_use('clam')
# Configure the row height (40 pixels)
style.configure('Treeview', rowheight=40)
# Create a Treeview widget
tree = ttk.Treeview(win, columns=("c1", "c2", "c3"), show='headings', height=5)
# Configure columns
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=('1', 'John', 'Smith'))
tree.insert('', 'end', values=('2', 'Emily', 'Johnson'))
tree.insert('', 'end', values=('3', 'Michael', 'Brown'))
tree.insert('', 'end', values=('4', 'Sarah', 'Davis'))
tree.insert('', 'end', values=('5', 'David', 'Wilson'))
tree.pack(pady=20)
win.mainloop()
Key Parameters
| Parameter | Description | Default Value |
|---|---|---|
rowheight |
Height of each row in pixels | 20 pixels |
theme |
Visual theme for the Treeview | System default |
Different Row Heights
You can experiment with different row heights to find what works best for your application ?
from tkinter import *
from tkinter import ttk
win = Tk()
win.geometry("800x400")
# Create style object
style = ttk.Style()
style.theme_use('clam')
# Try different row heights
heights = [20, 30, 50]
for i, height in enumerate(heights):
# Configure different row height for each treeview
style_name = f'Custom{i}.Treeview'
style.configure(style_name, rowheight=height)
# Create treeview with custom style
tree = ttk.Treeview(win, columns=("data",), show='headings', height=3, style=style_name)
tree.heading("data", text=f"Row Height: {height}px")
tree.column("data", anchor=CENTER, width=200)
# Add sample data
tree.insert('', 'end', values=(f'Row 1 - Height {height}',))
tree.insert('', 'end', values=(f'Row 2 - Height {height}',))
tree.insert('', 'end', values=(f'Row 3 - Height {height}',))
tree.pack(pady=10)
win.mainloop()
Output
Running the first example will display a Treeview with rows that are 40 pixels tall, providing more space for the data and improving readability.
Conclusion
Use ttk.Style().configure('Treeview', rowheight=value) to set custom row heights in Tkinter Treeview widgets. This improves readability and allows better accommodation of larger text or icons within table cells.
