Unbinding Tkinter default bindings for Listbox

James Stroud jstroud at mbi.ucla.edu
Mon Jan 12 12:27:52 CET 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/library/listbox.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::Priv(x) %x\n    set tk::Priv(y) %y\n    tk::ListboxMotion 
%W [%W index @%x,%y]\n'
py> b.tk.call('bind', 'Listbox', '<B1-Motion>', _)


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


More information about the Python-list mailing list