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!
For example you need to specify
and later
The value you give to the
columns=
argument isn't doing what you expect it to do.From the New Mexico Tech Tkinter reference:
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 usingcolumns = ('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.