Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python ttk treeview: how to select and set focus on a row?

Tags:

python

ttk

I have a ttk.Treeview widget with some rows of data. How do I set the focus to and select (highlight) a specified item?

tree.focus_set()

does nothing

tree.selection_set(0)

complains that: Item 0 not found, although the widget is plainly populated with more than zero items. Trying item 1 does no better.

EDIT: to select an item, find its id, then use tree.selection_set(id). Neither tree.focus(id) nor tree.focus_set(id) appears to do anything.

like image 633
foosion Avatar asked Oct 22 '11 20:10

foosion


2 Answers

Get the id of treeview item you want to highlight/select

child_id = tree.get_children()[-1] # for instance the last element in tuple

To highlight the item, use both focus() and selection_set(item_id)

tree.focus(child_id)
tree.selection_set(child_id)
like image 55
Cat Avatar answered Sep 26 '22 09:09

Cat


Note: I haven't worked with python.

Looking at this link, the focus method with optional parameter item, should highlight the node.

If not, look at selectmode option & set it to "browse".

like image 26
shahkalpesh Avatar answered Sep 23 '22 09:09

shahkalpesh