简体   繁体   English

tkinter - ttk 树视图:查看列文本

[英]tkinter - ttk treeview: see column text

I'm building a table in Tkinter using ttk's Treeview widget.我正在使用 ttk 的 Treeview 小部件在 Tkinter 中构建一个表。 However, after I inserted the columns they display it without text.但是,在我插入列后,它们显示它而没有文本。 Here is the code:这是代码:

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

Here the result:结果如下:

树状视图结果图像

How can I solve?我该如何解决?

Thanks谢谢

You need to define the headers for each column.您需要为每一列定义标题。 I don't know if you want to use the same column names for the header or not so that is going to be my example.我不知道你是否想对标题使用相同的列名,所以这将是我的例子。 You can change the text to whatever you want.您可以将文本更改为您想要的任何内容。 To define the header you will need to use heading() like this:要定义标题,您需要像这样使用heading()

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

With those changes your final code should look like this:通过这些更改,您的最终代码应如下所示:

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

Results:结果:

在此处输入图片说明

Let me know if you have any questions.如果您有任何问题,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM