Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Unbinding Tkinter default bindings for Listbox

Reply
Thread Tools

Unbinding Tkinter default bindings for Listbox

 
 
Roger
Guest
Posts: n/a
 
      01-11-2009
Hi Everyone,

I have a behavior associated with a default binding with Tkinter
Listbox that I want to get rid of but I can't no matter if I return
"break" on the binding or unbind it directly. If you have a Listbox
where the bounding box is not completely revealed in the window that
holds it and you use the mouse to drag the list box, the contents of
the listbox will move in the X direction to reveal the area that's
hidden. After searching the internet for hours I found the behavior I
want to remove is a default binding as described here:

http://tcltk.free.fr/man/TkCmd/listbox.php3

"[3] If the mouse leaves the listbox window with button 1 down, the
window scrolls away from the mouse, making information visible that
used to be off-screen on the side of the mouse. The scrolling
continues until the mouse re-enters the window, the button is
released, or the end of the listbox is reached. "

After further searching I found that the code for this in tcl is
described here:

http://www.openmash.org/lxr/source/l...ox.tcl?c=tk8.3

50 bind Listbox <B1-Motion> {
51 set tkPriv(x) %x
52 set tkPriv(y) %y
53 tkListboxMotion %W [%W index @%x,%y]
54 }

Now I've found no way to successfully unbind B1-Motion from the
listbox, as I said above. Nor return "break" on receiving the event.
I do want to eventually have my own B1-Motion binding applied to the
listbox for a different reason (with add='+' if necessary). My next
step was to see if I could just unbind B1-Motion then programmatically
delete tkListboxMotion in my code but tkListboxMotion is not available
in the Tkinter source (it's part of a compiled binary I can't reach?).

Any help would be greatly appreciated. Thanks!
Roger.
 
Reply With Quote
 
 
 
 
James Stroud
Guest
Posts: n/a
 
      01-12-2009
Roger wrote:
> Hi Everyone,
>
> I have a behavior associated with a default binding with Tkinter
> Listbox that I want to get rid of but I can't no matter if I return
> "break" on the binding or unbind it directly. If you have a Listbox
> where the bounding box is not completely revealed in the window that
> holds it and you use the mouse to drag the list box, the contents of
> the listbox will move in the X direction to reveal the area that's
> hidden. After searching the internet for hours I found the behavior I
> want to remove is a default binding as described here:
>
> http://tcltk.free.fr/man/TkCmd/listbox.php3
>
> "[3] If the mouse leaves the listbox window with button 1 down, the
> window scrolls away from the mouse, making information visible that
> used to be off-screen on the side of the mouse. The scrolling
> continues until the mouse re-enters the window, the button is
> released, or the end of the listbox is reached. "
>
> After further searching I found that the code for this in tcl is
> described here:
>
> http://www.openmash.org/lxr/source/l...ox.tcl?c=tk8.3
>
> 50 bind Listbox <B1-Motion> {
> 51 set tkPriv(x) %x
> 52 set tkPriv(y) %y
> 53 tkListboxMotion %W [%W index @%x,%y]
> 54 }
>
> Now I've found no way to successfully unbind B1-Motion from the
> listbox, as I said above. Nor return "break" on receiving the event.
> I do want to eventually have my own B1-Motion binding applied to the
> listbox for a different reason (with add='+' if necessary). My next
> step was to see if I could just unbind B1-Motion then programmatically
> delete tkListboxMotion in my code but tkListboxMotion is not available
> in the Tkinter source (it's part of a compiled binary I can't reach?).
>
> Any help would be greatly appreciated. Thanks!
> Roger.


You can directly send commands to the Tcl interpreter via the call
method of the tk attribute of any Tkinter widget. For example:


py> from Tkinter import *
py> tk = Tk()
py> b = Button(tk)
py> b.pack()
py> b.tk
<tkapp object at 0xd57720>
py> b.tk.call('bind', 'Listbox', '<B1-Motion>')
'\n set tk:riv(x) %x\n set tk:riv(y) %y\n tk::ListboxMotion
%W [%W index @%x,%y]\n'
py> b.tk.call('bind', 'Listbox', '<B1-Motion>', _)
''


James



--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com
 
Reply With Quote
 
 
 
 
James Stroud
Guest
Posts: n/a
 
      01-12-2009
James Stroud wrote:
> py> b.tk.call('bind', 'Listbox', '<B1-Motion>', _)


You want b.tk.call('bind', 'Listbox', '<B1-Motion>', "") of course.

James


--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com
 
Reply With Quote
 
Roger
Guest
Posts: n/a
 
      01-12-2009
On Jan 12, 6:31*am, James Stroud <(E-Mail Removed)> wrote:
> James Stroud wrote:
> > py> b.tk.call('bind', 'Listbox', '<B1-Motion>', _)

>
> You want b.tk.call('bind', 'Listbox', '<B1-Motion>', "") of course.
>
> James
>
> --
> James Stroud
> UCLA-DOE Institute for Genomics and Proteomics
> Box 951570
> Los Angeles, CA 90095
>
> http://www.jamesstroud.com


Knowing this is actually extremely useful (looking at Tkinter.py i see
this is equivalent to the unbind method). Unfortunately it's not
working to unbind B1-Motion from Listbox even though I get the same
output results as you've gotten above. =(

Thanks a ton James. This is still very educational to me.

Roger.
 
Reply With Quote
 
Roger
Guest
Posts: n/a
 
      01-12-2009
I'm sorry for harassing the list but any suggestions would be greatly
appreciated. =)
 
Reply With Quote
 
James Stroud
Guest
Posts: n/a
 
      01-12-2009
Roger wrote:
> I'm sorry for harassing the list but any suggestions would be greatly
> appreciated. =)


I just checked the behavior you are describing. It seems that you want
to unbind the <Leave> event to stop the autoscrolling when you leave the
listbox. E.g.:

from Tkinter import *
tk = Tk()
lb = Listbox(tk)
lb.pack()
tk.tk.call('bind', str(lb), '<Leave>', "break")
for i in range(12):
lb.insert(END, str(i))


Sorry for not reading more carefully.

James
 
Reply With Quote
 
James Stroud
Guest
Posts: n/a
 
      01-12-2009
James Stroud wrote:
> tk.tk.call('bind', str(lb), '<Leave>', "break")


Which is equivalent to

lb.bind('<Leave>', "break")

But I checked and overriding the default behavior of Listbox does not
work (tk.tk.call('bind', "Listbox", '<Leave>', "break")).

So I would subclass Listbox and do the bindings in the __init__:

class MyListbox(Listbox):
def __init__(self, *args, **kwargs):
Listbox.__init__(self, *args, **kwargs)
self.bind('<Leave>', "break")


James
 
Reply With Quote
 
Roger
Guest
Posts: n/a
 
      01-13-2009
On Jan 12, 5:58*pm, James Stroud <(E-Mail Removed)> wrote:
> James Stroud wrote:
> > tk.tk.call('bind', str(lb), '<Leave>', "break")

>
> Which is equivalent to
>
> lb.bind('<Leave>', "break")
>
> But I checked and overriding the default behavior of Listbox does not
> work (tk.tk.call('bind', "Listbox", '<Leave>', "break")).
>
> So I would subclass Listbox and do the bindings in the __init__:
>
> class MyListbox(Listbox):
> * *def __init__(self, *args, **kwargs):
> * * *Listbox.__init__(self, *args, **kwargs)
> * * *self.bind('<Leave>', "break")
>
> James


You sir are awesome. Thank you so much. I've so far been really good
at troubleshooting Tkinter, the library has really melded with my
thinking naturally since I started using Python on a regular basis a
year ago. I had mixed feelings about asking for help on this because
I wanted so badly to figure it out myself but I became so exasperated
especially after combing through the TCL source to no affect. Now
that you've given me the correct solution it seems like the Leave
event should have been a natural binding for me to break in all my
fiddling.

Ah well, it's a lesson I won't forget thanks to you. Thanks so much
again, you're help is invaluable. =D

Roger.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Tkinter unbinding Roger Python 17 12-04-2011 11:13 PM
Tkinter Listbox - Different Text colors in one listbox rahulnag22@yahoo.com Python 6 05-31-2007 06:48 AM
Tkinter default bindings Phil Schmidt Python 1 03-02-2004 05:28 AM
Re: UNbinding a socket Sin C++ 2 06-25-2003 06:27 PM
Re: (OT) UNbinding a socket Michael Furman C++ 0 06-23-2003 07:41 PM



Advertisments