Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding tkinter Treeview

I am trying to create a Treeview - here is my code:

def treeview(self):
        tree = Treeview(self.parent, columns=("no","name","props","subs", "summary"))

        tree.heading('#0', text='No.')
        tree.column('#0', width=100)
        tree.heading('#1', text='Name')
        tree.column('#1', width=100)
        tree.heading('#2', text='Props')
        tree.column('#2', width=100)
        tree.heading('#3', text='Subs')
        tree.column('#3', width=100)
        tree.heading('#4', text='Summary')
        tree.column('#4', width=100)

        tree.place(relx=0.5,rely=0.2, anchor=CENTER)

There should be 5 columns - However, there is sixth blank column to the right of the last column. Why is this?

Also, how do I actually add text to each column? Thanks!

like image 628
MortalMan Avatar asked Oct 31 '22 08:10

MortalMan


1 Answers

The 0'th column in a TreeView is always the "icon column" - see this manual page, where it says:

Your Treeview widget will be structured with multiple columns. The first column, which we'll call the icon column, displays the icons that collapse or expand items. In the remaining columns, you may display whatever information you like.

So my best guess would be that when you try to specify a label for column 0 Tk is adding one? (making the total 6).


As for adding text to the items of your Treeview.. Again, the manual page says:

Starting with the top-level entries, use the .insert() method to populate the tree. Each call to this method adds one item to the tree. Use the open keyword argument of this method to specify whether the item is initially expanded or collapsed.

If you want to supply the iid value for this item, use the iid keyword argument. If you omit this argument, ttk will make one up and return it as the result of the .insert() method call.

Which covers how to insert rows, then to "actually add text to each column":

values: This argument supplies the data items to be displayed in each column of the item. The values are supplied in logical column order. If too few values are supplied, the remaining columns will be blank in this item; if too many values are supplied, the extras will be discarded.

Thus, tree.insert(values=[3,"Fred",'prop','sub','guy named Fred']) would insert a row with the given values for the columns you have defined.

like image 167
Tersosauros Avatar answered Dec 24 '22 15:12

Tersosauros