Extend selection in ttk.Treeview

Vasilis Vlachoudis <[email protected]> Tue, 5 Jan 2021 11:06:24 +0000
Newsgroups gmane.comp.python.tkinter
Message-ID <[email protected]>
--===============4546493020808678005==
Content-Language: en-US
Content-Type: multipart/alternative;
	boundary="_000_0BC70B5D93E054469872FFD0FE07220E0312E0A1DCCERNXCHG51cer_"

--_000_0BC70B5D93E054469872FFD0FE07220E0312E0A1DCCERNXCHG51cer_
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Hi all,

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

Looking the treeview.tcl I've managed to add a binding for the B1-Motion wh=
ich 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) =3D=3D 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 =3D self.focus()
self.tk.call("ttk::treeview::SelectOp", self._w, item, "extend")
return "break"

--_000_0BC70B5D93E054469872FFD0FE07220E0312E0A1DCCERNXCHG51cer_
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<html dir=3D"ltr">
<head>
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Diso-8859-=
1">
<style type=3D"text/css" id=3D"owaParaStyle"></style>
</head>
<body fpstyle=3D"1" ocsi=3D"0">
<div style=3D"direction: ltr;font-family: Tahoma;color: #000000;font-size: =
10pt;">Hi all,
<div><br>
</div>
<div>I am currently replacing some Listbox() with ttk.Treeview() however th=
e Treeview doesn't have the</div>
<div>&quot;Shift-Up/Down&quot; and &quot;B1-Motion&quot; capabilities to se=
lect with the keyboard multiple items and with the mouse by click and drag.=
</div>
<div><br>
</div>
<div>Looking the treeview.tcl I've managed to add a binding for the B1-Moti=
on which seems to work,</div>
<div>however I failed to find a way to correctly replicate the Listbox beha=
vior with the Shift-Up/Down</div>
<div>This is one of my failed tries</div>
<div><br>
</div>
<div>Any suggestion?</div>
<div><br>
</div>
<div>class MyTreeview(ttk.TreeView):</div>
<div><span style=3D"white-space:pre"></span>def __init__(self, master, *kw,=
 **args):</div>
<div>
<div><span style=3D"font-size: 13.3333px;"><span style=3D"white-space:pre">=
</span>super().__init__(master, *kw, **args)</span></div>
<div><span style=3D"white-space: pre;"></span>self.bind(&quot;&lt;B1-Motion=
&gt;&quot;,<span style=3D"white-space: pre;">
</span>self.b1motion)</div>
<div>
<div><span style=3D"white-space: pre;"></span>self.bind(&quot;&lt;Shift-Key=
-Up&gt;&quot;,<span style=3D"white-space: pre;">
</span>self.selectUp)</div>
<div><span style=3D"font-size: 13.3333px;"><span style=3D"white-space:pre">=
</span>self.bind(&quot;&lt;Shift-Key-Down&gt;&quot;,<span style=3D"white-sp=
ace:pre">
</span>self.selectDown)</span></div>
</div>
<div><br>
</div>
</div>
<div><span style=3D"white-space:pre"></span>def b1motion(self, event):</div=
>
<div>
<div><span style=3D"font-size: 13.3333px;"><span style=3D"white-space:pre">=
</span>if self.identify_row(event.y) =3D=3D self.focus(): return</span></di=
v>
<div><span style=3D"white-space: pre;"></span>self.tk.call(&quot;ttk::treev=
iew::Select&quot;, self._w, event.x, event.y, &quot;extend&quot;)</div>
</div>
<div><br>
</div>
<div>
<div><span style=3D"white-space: pre;"></span>def selectDown(self, event):<=
/div>
<div><span style=3D"white-space: pre;"></span>self.tk.call(&quot;ttk::treev=
iew::Keynav&quot;, self._w, &quot;down&quot;)</div>
<div><span style=3D"font-size: 13.3333px;"><span style=3D"white-space:pre">=
</span>item =3D self.focus()</span></div>
<div><span style=3D"white-space: pre;"></span>self.tk.call(&quot;ttk::treev=
iew::SelectOp&quot;, self._w, item, &quot;extend&quot;)</div>
<div><span style=3D"font-size: 13.3333px;"><span style=3D"white-space:pre">=
</span>return &quot;break&quot;</span></div>
</div>
</div>
</body>
</html>

--_000_0BC70B5D93E054469872FFD0FE07220E0312E0A1DCCERNXCHG51cer_--

--===============4546493020808678005==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
Tkinter-discuss mailing list
[email protected]
https://mail.python.org/mailman/listinfo/tkinter-discuss

--===============4546493020808678005==--