Reputation: 755
I want to add three columns to my Treeview and name them '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.
Any ideas would be greatly appreciated!
Upvotes: 1
Views: 6978
Reputation: 8062
Using a for-loop:
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()
Upvotes: 3
Reputation: 10592
The value you give to the columns=
argument isn't doing what you expect it to do.
From the New Mexico Tech Tkinter reference:
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.The columns you specify with the columns argument are in addition to the icon column.
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'
.
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'
.
To explain the error you're getting, when you use columns = 3
this gives the same result as using columns = ('3')
would; you're actually only creating one column next to the '#0'
column, which can be identified by either '#1'
or '3'
. When you try to access column '#2'
you get an out of range error because there are only two columns.
Upvotes: 4
Reputation: 370
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')
Upvotes: 3