python 3 - tkinter - ttk treeview:查看列文本

问题描述 投票:2回答:1

我正在使用ttk的Treeview小部件在Tkinter中构建一个表。但是,在我插入列后,它们显示没有文本。这是代码:

w=Tk()
f=Frame(w)
f.pack()
t=Treeview(f,columns=("Titolo","Data","Allegati?"))
t.pack(padx=10,pady=10)
t.insert("",1,text="Sample")

结果如下:

我怎么解决?

谢谢

python python-3.x tkinter treeview ttk
1个回答
1
投票

您需要为每列定义标头。我不知道你是否想要为标题使用相同的列名,所以这将成为我的榜样。您可以将文本更改为您想要的任何内容。要定义标题,您需要像这样使用header()

t.heading("Titolo", text="Titolo")
t.heading("Data", text="Data")
t.heading("Allegati?", text="Allegati?")

通过这些更改,您的最终代码应如下所示:

from tkinter import *
from tkinter.ttk import *


w=Tk()

f = Frame(w)
f.pack()
t = Treeview(f, columns=("Titolo", "Data", "Allegati?"))

t.heading("Titolo", text="Titolo")
t.heading("Data", text="Data")
t.heading("Allegati?", text="Allegati?")

t.pack(padx=10, pady=10)
t.insert("", 1, text="Sample")

w.mainloop()

结果:

enter image description here

如果您有任何疑问,请告诉我。

© www.soinside.com 2019 - 2024. All rights reserved.