#๐Ÿ”’ Tkinter TTK Treeview - toggle selection when clicking item

15 messages ยท Page 1 of 1 (latest)

quick coyote
#

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

tender mantleBOT
#

@quick coyote

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

tender mantleBOT
#
Pasting large amounts of code

If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/

After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.

quick coyote
#

you'll need a png icon to run the script.. i didn't add backup icon code to create its own image

plush cargo
# quick coyote when the user clicks an item in the Treeview widget, the goal is to select the i...

looks like you can handle it more reliably if you disable the built-in selection behaviour with selectmode="none": ```py
from tkinter import Tk
from tkinter.ttk import Treeview

app = Tk()
tree = Treeview(app, show="tree", selectmode="none")
tree.pack(expand=True, fill="both")

for i in range(10):
tree.insert("", "end", text=f"Entry {i+1}")

def toggle_selection(event):
iid = tree.identify_row(event.y)
if iid not in tree.selection():
tree.selection_set(iid)
else:
tree.selection_set()

tree.bind("<1>", toggle_selection)
app.mainloop()```

#

assuming you don't need multi-select, that is

quick coyote
#

yeah i dont

#

you're the best ty!

#

!close

tender mantleBOT
#
Python help channel closed with !close

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.