python tkinter tree get selected item values -


i'm starting small tkinter tree program in python 3.4.

i'm stuck returning first value of row selected. have multiple rows 4 columns , calling function on left-click on item:

tree.bind('<button-1>', selectitem) 

the function:

def selectitem(a):     curitem = tree.focus()     print(curitem, a) 

this gives me this:

i003 <tkinter.event object @ 0x0179d130> 

it looks selected item gets identified correctly. need how first value in row.

tree-creation:

from tkinter import * 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() 

to selected item , attributes , values, can use item method:

def selectitem(a):     curitem = tree.focus()     print tree.item(curitem) 

this output dictionary, can retrieve individual values:

{'text': 'name', 'image': '', 'values': [u'date', u'time', u'loc'], 'open': 0, 'tags': ''} 

also note callback executed before focus in tree changed, i.e. item was selected before clicked new item. 1 way solve use event type buttonrelease instead.

tree.bind('<buttonrelease-1>', selectitem) 

Comments