tkinter - ttk 树视图:见列文本
tkinter - ttk treeview: see column text
我正在使用 ttk 的 Treeview 小部件在 Tkinter 中构建一个 table。但是,在我插入列后,它们会在没有文本的情况下显示。
这是代码:
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。我不知道您是否想对 header 使用相同的列名,所以这将是我的示例。您可以将文本更改为您想要的任何内容。要定义 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()
结果:
如果您有任何问题,请告诉我。
我正在使用 ttk 的 Treeview 小部件在 Tkinter 中构建一个 table。但是,在我插入列后,它们会在没有文本的情况下显示。 这是代码:
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。我不知道您是否想对 header 使用相同的列名,所以这将是我的示例。您可以将文本更改为您想要的任何内容。要定义 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()
结果:
如果您有任何问题,请告诉我。