如何在ttk Treeview小部件中将文本对齐?

时间:2015-05-22 10:00:23

标签: python tkinter ttk

我正在使用ttk.Treeview窗口小部件来显示阿拉伯图书列表。阿拉伯语是一种从右到左的语言,因此文本应该与右边对齐。 Label和其他ttk小部件可用的对齐选项似乎不适用于Treeview

有谁知道怎么做?

2 个答案:

答案 0 :(得分:5)

ttk.Treeview窗口小部件可以为每列设置anchor选项。要将列的锚点设置为右侧,请使用:

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

答案 1 :(得分:0)

除了@fhdrsdg答案之外,这里还有一个简单的使用示例:

# 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()

如果您需要有关如何使用ttk.Treeview小部件的更多帮助,请查看The New Mexico Tech的this reference或TkDocs的this tutorial