Skip to main content
 首页 » 编程设计

python之tkinter之ttk TreeView : see column text

2025年02月15日8kuangbin

我正在使用 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") 

结果如下:

我该如何解决?

谢谢

请您参考如下方法:

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

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

结果:

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