Python 设置tkinter.ttk.Treeview列中的文本格式

Python 设置tkinter.ttk.Treeview列中的文本格式,python,python-3.x,treeview,tkinter,ttk,Python,Python 3.x,Treeview,Tkinter,Ttk,我想知道如何在ttk.Treeview列中证明文本的合理性。下面是我的意思的一个例子。请注意日期以及数字之间的位置不正确。我想这和间距有关,但我可能错了 编辑:它是用Python 3编写的 #! coding=utf-8 import pickle import matplotlib.pyplot as plt import tkinter as tk from tkinter import ttk # Create Example root = tk.Tk() root.minsize(20

我想知道如何在ttk.Treeview列中证明文本的合理性。下面是我的意思的一个例子。请注意日期以及数字之间的位置不正确。我想这和间距有关,但我可能错了

编辑:它是用Python 3编写的

#! coding=utf-8
import pickle
import matplotlib.pyplot as plt
import tkinter as tk
from tkinter import ttk

# Create Example
root = tk.Tk()
root.minsize(200,300)
tree = ttk.Treeview(root,columns=("date"))
tree.heading("#0"  , text='Sample', anchor=tk.W)
tree.column("#0", stretch=0)
tree.heading("date", text='Date', anchor=tk.E)
tree.column("date", stretch=0)

ABC   = ["A","B","C","D","E"]
dates = ["3.4.2013", "14.10.400", "24.12.1234", "1.10.1", "14.7.123"]
tree.insert("",iid="1", index="end",text="No Format")
for i in range(len(ABC)):
dates2 = dates[i].split(".")
    date   = "{:<2}.{:<2}.{:<4}".format(dates2[0],dates2[1],dates2[2])
    tree.insert("1",iid="1"+str(i), index="end",text=ABC[i], value=[dates[i]])
tree.see("14")
tree.insert("",iid="2", index="end",text="With Format")
for i in range(len(ABC)):
    dates2 = dates[i].split(".")
    date   = "{:>2}.{:>2}.{:>4}".format(dates2[0],dates2[1],dates2[2])
    tree.insert("2",iid="2"+str(i), index="end",text=ABC[i], value=[date])
tree.see("24")

tree.pack(expand=True,fill=tk.BOTH)

root.mainloop()
#!编码=utf-8
进口泡菜
将matplotlib.pyplot作为plt导入
将tkinter作为tk导入
从tkinter导入ttk
#创建示例
root=tk.tk()
root.minsize(200300)
tree=ttk.Treeview(根,列=(“日期”))
树标题(“0”,text='Sample',anchor=tk.W)
tree.column(“#0”,stretch=0)
树标题(“日期”,text='date',anchor=tk.E)
tree.column(“日期”,拉伸=0)
ABC=[“A”、“B”、“C”、“D”、“E”]
日期=[“3.4.2013”、“14.10.400”、“24.12.1234”、“1.10.1”、“14.7.123”]
tree.insert(“,iid=“1”,index=“end”,text=“无格式”)
对于范围内的i(len(ABC)):
dates2=日期[i]。拆分(“.”)
date=“{:4}”。格式(dates2[0],dates2[1],dates2[2])
插入(“2”,iid=“2”+str(i),index=“end”,text=ABC[i],value=[date])
树。见(“24”)
tree.pack(expand=True,fill=tk.BOTH)
root.mainloop()
使用以下单空格字体:

。。。
对于范围内的i(len(ABC)):
dates2=日期[i]。拆分(“.”)
date=“{:>2}.{:>2}.{:>4}”。格式(dates2[0],dates2[1],dates2[2])
tree.insert(“2”,iid=“2”+str(i),index=“end”,text=ABC[i],value=[date],

tag='monospace')#您可以使用锚定选项对日期列中的文本进行对正,方法与对日期标题中的文本进行对正相同

tree.heading("date", text='Date', anchor=tk.E)
tree.column("date", stretch=0, anchor=tk.E)

有关
标题
方法的锚定和其他选项的更多详细信息,请参见新墨西哥理工大学的。

Hm,这确实有效,但现在不同的行有不同的字体大小,除非我采用这种方式格式化。是否有调整fontsize和/或为每列指定fontsize的选项?我在标记选项中没有看到类似的内容。@throway17434,您不需要多次调用
tree.tag\u configure
。叫它一次。(即使每个
树都需要
tag='monospace'
。插入
tree.heading("date", text='Date', anchor=tk.E)
tree.column("date", stretch=0, anchor=tk.E)