[英]Tkinter - Adding multiple columns to a Treeview
I want to add three columns to my Treeview and name them 'Varenavn','Antall','Best før'.我想向我的 Treeview 添加三列,并将它们命名为“Varenavn”、“Antall”、“Best før”。 I tried the following:我尝试了以下方法:
self.tree = ttk.Treeview (height = 10, columns = 3)
self.tree.grid (row = 4, column = 0, columnspan = 2)
self.tree.heading ('#0', text = 'Varenavn', anchor = W)
self.tree.heading ('#1', text = 'Antall', anchor = W)
self.tree.heading ('#2', text = 'Best før', anchor = W)
But I am getting:但我得到:
_tkinter.TclError: Column #2 out of range.
If I change the last bit of code to:如果我将最后一点代码更改为:
self.tree.heading ('#1', text = 'Best før', anchor = W)
The code runs fine, but overwrites 'Antall'
to 'Best før'
in the second column.代码运行良好,但在第二列'Best før'
'Antall'
覆盖为'Best før'
。
Any ideas would be greatly appreciated!任何想法将不胜感激!
The value you give to the columns=
argument isn't doing what you expect it to do.您为columns=
参数提供的值没有按照您的预期执行。
From the New Mexico Tech Tkinter reference :来自新墨西哥技术 Tkinter 参考:
columns列
A sequence of column identifier strings.列标识符字符串序列。 These strings are used internally to identify the columns within the widget.这些字符串在内部用于标识小部件中的列。 The icon column, whose identifier is always
'#0'
, contains the collapse/expand icons and is always the first column.图标列的标识符始终为'#0'
,包含折叠/展开图标并且始终是第一列。The columns you specify with the columns argument are in addition to the icon column.您使用 columns 参数指定的列是对图标列的补充。
For example, if you specified
columns=('Name', 'Size')
, three columns would appear in the widget: first the icon column, then two more columns whose internal identifiers are'Name'
and'Size'
.例如,如果您指定columns=('Name', 'Size')
,小部件中将出现三列:首先是图标列,然后是另外两列,其内部标识符为'Name'
和'Size'
。
So instead of a number, you should give it a tuple of names for the columns you want to create, and you should give one less than the total number of columns you want, since the first is always '#0'
.因此,您应该为要创建的列指定一个名称元组,而不是一个数字,并且您应该给出一个比您想要的列总数少一个,因为第一个始终是'#0'
。
To explain the error you're getting, when you use columns = 3
this gives the same result as using columns = ('3')
would;为了解释你得到的错误,当你使用columns = 3
这与使用columns = ('3')
给出的结果相同; you're actually only creating one column next to the '#0'
column, which can be identified by either '#1'
or '3'
.您实际上只是在'#0'
列旁边创建了一个列,该列可以由'#1'
或'3'
标识。 When you try to access column '#2'
you get an out of range error because there are only two columns.当您尝试访问列'#2'
您会收到超出范围的错误,因为只有两列。
For example you need to specify例如,您需要指定
self.tablex=ttk.Treeview(heigh=10,columns=("#0","#1","#2","#3"))
and later然后
self.tablex.heading('#0',text='Text0')
self.tablex.heading('#1',text='Text1')
self.tablex.heading('#2',text='Text2')
self.tablex.heading('#3',text='Text3')
Using a for-loop:使用 for 循环:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
tree = ttk.Treeview(root,columns=())
tree.pack(fill='x')
for i in range(5):
param = tree['columns']+(f'#{i+1}',)
tree.configure(columns=param)
for i in range(5):
tree.heading(column=f'#{i+1}',text=f'text{i+1}',anchor='w')
tree.column(column=f'#{i+1}', width=150,minwidth=50,stretch=False)
root.mainloop()
Using a list:使用列表:
import tkinter as tk
from tkinter import ttk
cols = ['one','two','three','four','five']
root = tk.Tk()
tree = ttk.Treeview(root,columns=cols)
tree.pack(fill='x')
for i in cols:
tree.heading(column=f'{i}',text=f'{i}',anchor='w')
tree.column(column=f'{i}', width=150,minwidth=50,stretch=False)
root.mainloop()
manually:手动:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
tree = ttk.Treeview(root,columns=('one','two','three','four','five'))
tree.pack(fill='x')
for i in tree['columns']:
tree.heading(column=f'{i}',text=f'{i}',anchor='w')
tree.column(column=f'{i}', width=150,minwidth=50,stretch=False)
root.mainloop()
generic:通用的:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
tree = ttk.Treeview(root,columns=tuple(f"{i}" for i in range(5)))
tree.pack(fill='x')
for i in tree['columns']:
tree.heading(column=f'{i}',text=f'{i}',anchor='w')
tree.column(column=f'{i}', width=150,minwidth=50,stretch=False)
root.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.