tkinter Treeview: get selected item values
Asked Answered
E

4

22

I'm just starting with a small tkinter tree program in python 3.4.

I'm stuck with returning the first value of the row selected. I have multiple rows with 4 columns and I am calling a function on left-click on a item:

tree.bind('<Button-1>', selectItem)

The function:

def selectItem(a):
    curItem = tree.focus()
    print(curItem, a)

This gives me something like this:

I003 <tkinter.Event object at 0x0179D130>

It looks like the selected item gets identified correctly. All I need now is how to get the first value in the row.

tree-creation:

from tkinter import *
from tkinter import ttk

def selectItem():
    pass

root = Tk()
tree = ttk.Treeview(root, columns=("size", "modified"))
tree["columns"] = ("date", "time", "loc")

tree.column("date", width=65)
tree.column("time", width=40)
tree.column("loc", width=100)

tree.heading("date", text="Date")
tree.heading("time", text="Time")
tree.heading("loc", text="Loc")
tree.bind('<Button-1>', selectItem)

tree.insert("","end",text = "Name",values = ("Date","Time","Loc"))

tree.grid()
root.mainloop()
Estate answered 3/6, 2015 at 8:6 Comment(4)
Could you add the code for creating the tree? (or something shorter but similar if it is too long)Enterectomy
added the tree-creation script. It's not my whole program but the basi cpart of it.Estate
It seems like the callback is executed before the focus in the tree changes, so you always get the previously selected value.Enterectomy
Thanks for the advice, that might be true, but the first problem I have is still : how du i return the value in the first column of the selected row?Estate
E
56

To get the selected item and all its attributes and values, you can use the item method:

def selectItem(a):
    curItem = tree.focus()
    print tree.item(curItem)

This will output a dictionary, from which you can then easily retrieve individual values:

{'text': 'Name', 'image': '', 'values': [u'Date', u'Time', u'Loc'], 'open': 0, 'tags': ''}

Also note that the callback will be executed before the focus in the tree changed, i.e. you will get the item that was selected before you clicked the new item. One way to solve this is to use the event type ButtonRelease instead.

tree.bind('<ButtonRelease-1>', selectItem)
Enterectomy answered 3/6, 2015 at 9:6 Comment(4)
how to get particular value from output dictionary? can you please explain.Self
Perfect, simple, usefulAnytime
Nisarg Parekh: tree.item(curItem, 'text') and tree.item(curItem)['text'] will both retrieve the 'text' element for that item. If you want to alter the other columns one at a time, instead of using tree.item(curItem, 'values') or tree.item(curItem, values=['new', 'values', 'here']) you can use tree.set(curItem, 'time') to retrieve or tree.set(curItem, time='value') to change.Recommendation
ttk.treeview.focus() returns the current focus item. That means the item that was last selected. ttk.treeview.selection() returns a tuple of all the selected items.Lamanna
B
4

This is a good example of getting information of a row selected in a python tkinter treeview. Allow me to represent a final neat coding discussed here. I use python 3.8

from tkinter import *
from tkinter import ttk

def selectItem(a):
    curItem = tree.focus()
    print(tree.item(curItem))

root = Tk()
tree = ttk.Treeview(root, columns=("size", "modified"))
tree["columns"] = ("date", "time", "loc")

tree.column("date", width=65)
tree.column("time", width=40)
tree.column("loc", width=100)

tree.heading("date", text="Date")
tree.heading("time", text="Time")
tree.heading("loc", text="Loc")
tree.bind('<ButtonRelease-1>', selectItem)

tree.insert("","end",text = "Name",values = ("Date","Time","Loc"))

tree.grid()
root.mainloop()

Ther result is

{'text': 'Name', 'image': '', 'values': ['Date', 'Time', 'Loc'], 'open': 0, 'tags': ''}

You can copy, paste and try it. It's good.

Bridgers answered 17/9, 2020 at 10:23 Comment(0)
O
0

And if you want to get row as {column name:value pair}:

def selectItem(a):
    curRow = tree.set(a)
    loc_value = curRow["loc"]

Or you want to get cell value by column name

def selectItem(a):
    loc_value = tree.set(a, column="loc")
Oversubtle answered 17/8, 2021 at 18:48 Comment(0)
L
0

ttk.treeview.focus() returns the current focus item. That means the item that was last selected.

ttk.treeview.selection() returns a tuple of all the selected items.

Lamanna answered 10/10, 2023 at 10:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.