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

元气小坏坏 提交于 2021-02-04 18:13:45

问题


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?


回答1:


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)



回答2:


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.



来源:https://stackoverflow.com/questions/30393763/how-to-align-text-to-the-right-in-ttk-treeview-widget

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!