I'm having trouble trying to deselect a Treeview
item while doing it from a callback of another widget. I managed to implement a working deselection on ESCAPE press with:
tree.selection_remove(tree.focus())
in a callback bound to a treeview object itself. It works only when the treeview has focus (but it's OK, that's a desired behavior). On the other hand trying the same from a callback of another widget doesn't work.
I tried using:
tree.selection_clear()
as recommended here, but this does not work either.
Important: selecting items in the treeview from the same call with tree.selection_add(iid)
works. The same as manipulating yet another widget (a combobox, and the widget doing the call is a frame). Based on this, I don't suppose the problem is due to some silly bug (a typo or something like that) in my code.
My theory is being out of focus is a problem as it's the only difference I see between when the same code works with ESCAPE but not elsewhere, so I tried to gain focus with tree.focus_set()
and tree.focus_force()
, but that didn't change anything.
I don't quite understand what selection_clear()
is supposed to do. As ttk.Treeview
inherits this method from ttk.Misc
the documentation on this is scarse. I couldn't find anything on it here or here. The only thing I found is a laconic:
Clear the current X selection.
from the Python shell help (what's the cryptic X
, coordinates?)
So how to accomplish so seemingly simple? Please help, I'm at a loss for ideas.
OK. So the answer was:
ttk.Treeview.selection()
It's not listed in the great New Mexico Tech resource, but it does get mentioned in the best tutorial out there and (of course) is in the docs. Somehow it still escaped me.
Now, having a way to test contents of a selection it was easy:
if len(tree.selection()) > 0:
tree.selection_remove(tree.selection()[0])