Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning Tkinter Treeview iid

I have a treeview and have inserted some data into it as shown below.

self.tree.insert('', 'end', iid="test1", text="test a", values=("data1", "data2"))

This adds an entry to the end of the treeview with text "test a" and column values of "data1" and "data2".

The iid has been set to "test1".

I would like to return the value of iid of an item in the treeview so that I can use it for some other function (I will be storing a file path in this iid)

I tried using treeview.item() and this returned the following dictionary without the iid:

{'version': 'data1', 'author': 'data2'}

(where version and author are the column headings)

So my question: is there a simple way to return the iid of a given row/entry to the treeview?

like image 579
Cov Avatar asked Sep 06 '25 03:09

Cov


2 Answers

Learning Python, found no answer to this question here that I liked, so I finally figured out what I wanted and thought I would post what I found. iid = tree.focus() returns the information requested.

in my treeview i added this code:

for item in tree.selection():
    item_child = tree.get_children(item)
    item = tree.item(item)
    iid = tree.focus()
    print ("iid = ", iid, "Item = ", item, "Child iid's =", item_child)

this is my result from the code:

{'text': 'TRAINS', 'image': '', 'values': '', 'open': 0, 'tags': ['m']} iid = 1 Item = {'text': 'TRAINS', 'image': '', 'values': '', 'open': 0, 'tags': ['m']} Child iid's = ('8', '9', '10')

Notice at the beginning of line two of the result the iid is listed as "1" which is the correct iid for the item. All treeview items I select returned the correct iid.

Hope this helps the next person a little.

like image 196
jbt Avatar answered Sep 07 '25 16:09

jbt


To get the iid you can use the identify() function:

tree.identify(event.x, event.y) # item at row "x" and column "y"

The iid argument stands for item identifier which is unique for each item, and you can give it the value you want.

like image 31
Billal Begueradj Avatar answered Sep 07 '25 17:09

Billal Begueradj