peterv
peterv

Reputation: 167

How to align text to the right in ttk Treeview widget?

I am using a ttk.Treeview widget to display a list of Arabic books. Arabic is a right-to-left language, so the text should be aligned to the right. The justify option that is available for Label and other ttk widgets does not seem to work for Treeview.

Does anyone know how to do this?

Upvotes: 8

Views: 22671

Answers (2)

nbro
nbro

Reputation: 15878

In addition to the @fhdrsdg answer, here you have a simple example of use:

# for python 3
import tkinter as tk  
from tkinter import ttk
from tkinter import messagebox


def show_info():
    messagebox.showinfo("More info", "First column represents the subject" \
                        " and the second represents its corresponding " \
                        "current number of tagged questions on Stack Overflow.")

root = tk.Tk()

tree = ttk.Treeview(root, columns=("Tags"), height=6)

subjects = {"Tkinter": "8,013",
            "Python": "425,865",
            "C++": "369,851",
            "Java": "858,459"}

for subject in subjects.keys():
    tree.insert("", "end", text=subject, values=(subjects[subject]))

tree.column("Tags", anchor="e")    
tree.pack(fill="both", expand=True)

informer = tk.Button(root, text="More info", command=show_info)
informer.pack(side="bottom")


root.mainloop()

If you need more help on how to use ttk.Treeview widgets, have a look at this reference by The New Mexico Tech or this tutorial at TkDocs.

Upvotes: 3

fhdrsdg
fhdrsdg

Reputation: 10602

The ttk.Treeview widget has an anchor option you can set for each column. To set the anchor of a column to the right side use:

ttk.Treeview.column(column_id, anchor=Tkinter.E)

Upvotes: 15

Related Questions