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.
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.
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.
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.