python - tkinter - ttk TreeView : see column text

标签 python python-3.x tkinter treeview ttk

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

结果如下:

Treeview Result Image

如何解决?

谢谢

最佳答案

您需要定义每列的标题。我不知道您是否想对标题使用相同的列名称,所以这将是我的示例。您可以将文本更改为您想要的任何内容。要定义标题,您需要使用 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()

结果:

enter image description here

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

关于python - tkinter - ttk TreeView : see column text,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46056822/

相关文章:

python - 使用 python 列表初始化

python - 使用剪贴板时如何检查用户右键单击了哪个输入框?

python - 为什么 ndarray 允许浮点索引

python-3.x - 如何修复 Plotly Dash 中的 'Dropdown Menu Read' 错误

python - 组合当时列表 3 的元素

python - 类中的全局变量共享给其他实例

python - 如何获取调用事件的小部件的 ID (Tkinter)

python - Tkinter 在 Frames 中添加菜单栏

python - 如何使用 python 保存乌尔都语推文并保存在 csv 文件中

python - 如何用无序简化 "for x in a for y in b for z in c ..."?