How to cancel a tkinter key bind event

Question:

I have a tkinter treeview.
I am responding to a click on an item.
If the item is not unique i want to cancel the selection and stay on the current item.

The code I am using is:

def select_item(self, event, *args) -> None:
    item = self.index.identify_row(event.y)
    current_idx = self.index.index(item)
    if item:
        if self.current_item_index >= 0:
            if self.save_item(self.current_item_index):
                print("UNIQUE")
                self.current_item_index = current_idx
                self.load_item(current_idx)
            else:
                print("NOT UNIQUE", self.current_item_index)
                child_id = self.index.get_children()[self.current_item_index]
                self.index.focus(child_id)
                self.index.selection_set(child_id)

However, even after the test fails and I refocus on the current item, instead of the new selection, the new item is still selected.

For example; say I am currently editing the fourth item in the tree view. If the associated data is not unique, then if someone selects, say the seventh item, i want to ignore that selection and stay on the fourth item.

Asked By: Cromwell1963

||

Answers:

If your bound function returns the string "break", any further processing of the event will be cancelled. This includes cancelling the default behavior of the event.

Answered By: Bryan Oakley
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.