[Tkinter-discuss] Extend selection in ttk.Treeview

Vasilis Vlachoudis Vasilis.Vlachoudis at cern.ch
Tue Jan 5 06:06:24 EST 2021

Hi all,

I am currently replacing some Listbox() with ttk.Treeview() however the Treeview doesn't have the
"Shift-Up/Down" and "B1-Motion" capabilities to select with the keyboard multiple items and with the mouse by click and drag.

Looking the treeview.tcl I've managed to add a binding for the B1-Motion which seems to work,
however I failed to find a way to correctly replicate the Listbox behavior with the Shift-Up/Down
This is one of my failed tries

Any suggestion?

class MyTreeview(ttk.TreeView):
def __init__(self, master, *kw, **args):
super().__init__(master, *kw, **args)
self.bind("<B1-Motion>", self.b1motion)
self.bind("<Shift-Key-Up>", self.selectUp)
self.bind("<Shift-Key-Down>", self.selectDown)

def b1motion(self, event):
if self.identify_row(event.y) == self.focus(): return
self.tk.call("ttk::treeview::Select", self._w, event.x, event.y, "extend")

def selectDown(self, event):
self.tk.call("ttk::treeview::Keynav", self._w, "down")
item = self.focus()
self.tk.call("ttk::treeview::SelectOp", self._w, item, "extend")
return "break"
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.python.org/pipermail/tkinter-discuss/attachments/20210105/fef4ecdd/attachment.html>

More information about the Tkinter-discuss mailing list