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 align text to the right in ttk Treeview widget?
The Treeview widget displays data in a hierarchical table structure with rows and columns. By default, text in Treeview columns is left-aligned, but you can control alignment using the anchor property.
To align text to the right in a ttk Treeview widget, use the anchor parameter with the value "E" (East) when configuring columns. This property controls the position of text within each column cell.
Syntax
tree.column("column_id", anchor="E")
Anchor Values
The anchor parameter accepts the following values ?
| Value | Alignment | Description |
|---|---|---|
"W" |
Left | West (default) |
"CENTER" |
Center | Middle alignment |
"E" |
Right | East alignment |
Example
Here's how to create a Treeview with right-aligned text ?
# 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("Right-Aligned Treeview Example")
# Create an instance of Style widget
style = ttk.Style()
style.theme_use('clam')
# Add a Treeview widget
tree = ttk.Treeview(win, columns=("c1", "c2"), show='headings', height=8)
# Configure columns with right alignment (anchor="E")
tree.column("# 1", anchor="E", stretch=NO, width=100)
tree.heading("# 1", text="Index")
tree.column("# 2", anchor="E", 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 above code creates a Treeview widget where all text in both columns is aligned to the right ?
Mixed Alignment Example
You can also use different alignments for different columns ?
from tkinter import *
from tkinter import ttk
win = Tk()
win.geometry("600x300")
tree = ttk.Treeview(win, columns=("c1", "c2", "c3"), show='headings', height=6)
# Different alignments for each column
tree.column("# 1", anchor="W", width=80) # Left aligned
tree.heading("# 1", text="ID")
tree.column("# 2", anchor="CENTER", width=150) # Center aligned
tree.heading("# 2", text="Name")
tree.column("# 3", anchor="E", width=100) # Right aligned
tree.heading("# 3", text="Score")
# Insert sample data
tree.insert('', 'end', values=('001', 'Alice', '95'))
tree.insert('', 'end', values=('002', 'Bob', '87'))
tree.insert('', 'end', values=('003', 'Charlie', '92'))
tree.pack(pady=20)
win.mainloop()
Key Points
- Use
anchor="E"in thecolumn()- Apply alignment when configuring columns, not when inserting data
- Different columns can have different alignments in the same Treeview
- The alignment affects both data and header text positioning
Conclusion
To align text to the right in a ttk Treeview widget, use anchor="E" when configuring columns. This provides better visual organization for numerical data or when you need specific text positioning in your table display.
