when the user clicks an item in the Treeview widget, the goal is to select the item if it's not already selected or de-select it if it selected.. sounds simple, right?
def toggle_selection(self, event):
"""Toggle selection between the clicked item and no selection."""
self.click += 1
print('-' * 20, self.click, '-' * 20)
# Identify the row that was clicked
item = self.tree.identify_row(event.y)
print(f'Item: {item}')
if item:
current_selection = self.tree.selection()
print(f'Current selection: {current_selection}')
# If the clicked item is already selected, clear the selection
if item in current_selection:
print('yes, item is in current selection')
for i in self.tree.selection():
print(f'Item: {i}')
self.tree.selection_remove(i)
else:
# Otherwise, select only the clicked item
print('no, item is not in the current selection')
self.tree.selection_set(item)
# Update toolbar button states after changing selection
self.update_toolbar_button_states()
else:
print('no item')
i made a video showing how it seems like the faster i click the item the more accurately it works but if i click the item slowly it's not accurate.. so odd