katze
katze

Reputation: 1333

Get data from treeview in tkinter

I'm trying to get data from a Treeview I filled with :

self.tree['show'] = 'headings'
    self.tree['columns'] = ('Pool ID','Time', 'Lat', 'Lon', 'Alt', 'Spd', 'hdop','vdop','pdop', 'Sats Used', 'FixMode')
    self.tree.column('Pool ID', width=90, anchor='center')
    self.tree.heading('Pool ID', text='Pool ID')
    self.tree.column('Time', width=90, anchor='center')
    self.tree.heading('Time', text='Time')
    self.tree.column('Lat', width=90, anchor='center')
    self.tree.heading('Lat', text='Lat')
    self.tree.column('Lon', width=90, anchor='center')
    self.tree.heading('Lon', text='Lon')
    self.tree.column('Alt', width=90, anchor='center')
    self.tree.heading('Alt', text='Alt')
    self.tree.column('Spd', width=90, anchor='center')
    self.tree.heading('Spd', text='Spd')
    self.tree.column('hdop', width=90, anchor='center')
    self.tree.heading('hdop', text='hdop')
    self.tree.column('vdop', width=90, anchor='center')
    self.tree.heading('vdop', text='vdop')
    self.tree.column('pdop', width=90, anchor='center')
    self.tree.heading('pdop', text='pdop')
    self.tree.column('Sats Used', width=90, anchor='center')
    self.tree.heading('Sats Used', text='Sats Used')
    self.tree.column('FixMode', width=90, anchor='center')
    self.tree.heading('FixMode', text='FixMode')

    for i in range(5):

        self.tree.insert('', -1, values=(
                                           i+1,
                                           i+2, 
                                           i+3, 
                                           i+4,
                                           i+5,
                                           i+6,
                                           i+7,
                                           i+8,
                                           i+9,
                                           i+10,
                                           i+11))

I searched in the doc and on SO, but I didn't find anything about it. Can someone help me?

Upvotes: 3

Views: 18109

Answers (3)

I guess that you want to access the values of the tree rather than the text. I'll explain the difference later.

If you have a parent-child hierarchy, you use this code:

for parent in self.tree.get_children():
    print(self.tree.item(parent)["values"])
    for child in self.tree.get_children(parent):
        print(self.tree.item(child)["values"])

If you have a simple hierarchy, you use this code:

for parent in self.tree.get_children():
    print(self.tree.item(parent)["values"])

Now, you get the logic and you can go wild with parent-child-subchild hierarchy.

In addition to this, you might want to populate the tree via loop, instead of repeating the same code:

self.tree['columns'] = ('Pool ID','Time', 'Lat', 'Lon', 'Alt', 'Spd', 'hdop','vdop','pdop', 'Sats Used', 'FixMode')
for column in self.tree['columns']:
    # define the column names and headings
    self.tree.column(column, width=90, anchor='center')
    self.tree.heading(column, text=column)

Then, you might want to insert the data into the tree. You can do this also via loop, which is much more compact:

# let's replicate your example data set
data = [[i+k+1  for k in range(11)] for i in range(5)]
for r, row in data:
    self.tree.insert(parent='', index='end', iid=f"{str(r)}", text='', values=row)

Explanation: Now you can grab the values with print(self.tree.item(parent)["values"]), and if you use "text" instead of "values", you get whatever you defined in the text field in the self.tree.insert method. The iid defines the unique id of the entry in the tree: this is inserted programmatically. You can eventually fill the field text and get the content later. I left the field parent blank in this simple case. But if you have a parent-child hierarchy you should name it.

For example:

for p, parent_record in enumerate(data):
    self.tree.insert(parent='', ...)  # can leave this blank
    for r, row in enumerate(parent_record['rows']):
        self.tree.insert(parent=f"{str(p)}", ...)  # any reference to parent loop

Upvotes: 1

katze
katze

Reputation: 1333

Ok, I finally found how to do it, for example to display all values :

for line in self.tree.get_children():

    for value in self.tree.item(line)['values']:
        print(value)

Upvotes: 10

Jineesh Narayanan
Jineesh Narayanan

Reputation: 1

Use this code:

for Parent in self.treeview.get_children():

    print(self.treeview.item(Parent)["text"])

    for child in self.treeview.get_children(Parent):
        data = self.treeview.item(child)["text"]
        print(data)

Upvotes: 0

Related Questions