Tom Boy
Tom Boy

Reputation: 659

TreeView option to select and de-select by clicking?

I have been looking for a treeView widget option where I can select only a single row at a time by clicking it and then de-select it by clicking on it again. I found how to make it so that you can select only one row at a time, but I was unable to find an option that allowed u to click the selected row to de-select it.

Does anyone know how this can be done? Any help would be appreciated.

Upvotes: 2

Views: 2908

Answers (2)

Bryan Oakley
Bryan Oakley

Reputation: 385830

You can set a custom binding to deselect the item if it's currently selected. If your binding returns the string break, it will stop event propagation and thus prevent the default behavior for double-click.

...
self.tree = ttk.Treeview(...)
self.tree.bind("<1>", self.on_click)
...
def on_click(self, event):
    selection = self.tree.selection()
    item = self.tree.select_row(event.y)
    if item in selection:
        self.tree.selection_remove(item)
        return "break"

Upvotes: 3

Andath
Andath

Reputation: 22714

Unfortunately, the only built-in pattern to toggle the selection state of a treeview item is by selecting a different item but you can do a hack of your own.

Upvotes: 0

Related Questions