Victor Domingos Victor Domingos - 2 years ago 723
Python Question

How to get tkinter treeview selected row values using keyboard?

Based on the answer to a previous question from another user, I was able to get the correct cell values for each row on mouse release, binding that event to the selectItem() function as described there.

def selectItem(self, event):
curItem = self.tree.focus()

I also tried to add keyboard bindings for Up/Down keys, but I am not getting the actual selected row.

self.tree.bind('<ButtonRelease-1>', self.selectItem)
enter code here`self.tree.bind('<Up>', self.selectItem)
self.tree.bind('<Down>', self.selectItem)

Instead, when I press Up or Down, the selectItem function is printing the previous selected item. I mean, if I had selected row 3 and the press Up key, I still get row 3, but what I want is to get row 2. And if I press Down key, I want row 4, but I still get the contents of row 3. The blue row selection color is updating correctly, however. It seems that somehow the computer is starting by "reading" the current selected position and then move up or down. But I would like it to move first the selection and only "read" the current position at that time. What's the best way to accomplish this?

Answer Source

You can bind on <<TreeviewSelect>> instead. Binding to <<TreeviewSelect>> will let you get the item after the selection (of the treeview) has been set. Otherwise, binding to widgets vs bindings on the widgets class can override one another.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download