How do I bind <MouseWheel> to scrollbar?

Discussion in 'Python' started by Nicholas Shewmaker, Dec 14, 2005.

  1. (I apologize if this posts twice. My AVG is being fussy.)

    From what I've read, MouseWheel is a very tricky event. I have
    replaced my Python tcl84.dll and tk84.dll files with those in the
    ActiveTcl distribution to fix the crashes caused by the event. Then, I
    managed to see that my test script (see code below) was registering the
    event by printing the delta value (+/- 120).

    -----------------
    from Tkinter import *

    def reportScroll(event):
    print "scrolled", event.delta, event

    root = Tk()

    myListbox = Listbox(root, selectmode=SINGLE, height=10, width=20)
    for x in range(30):
    myListbox.insert(END, x)
    myListbox.pack(side=LEFT, expand=YES, fill=BOTH)

    myScrollbar = Scrollbar(root, command=myListbox.yview)
    myScrollbar.bind('<MouseWheel>', reportScroll)
    myScrollbar.focus_set()
    myScrollbar.pack(side=RIGHT, fill=Y)

    myListbox.config(yscrollcommand=myScrollbar.set)

    root.mainloop()
    -----------------

    The scrollbar and listbox are tied together, but moving the wheelmouse
    does nothing. I realize the code above doesn't give the event any real
    duties, but I'm not really sure how to make that leap. How and to what
    do I bind <WheelMouse> so that it will affect the listbox and the scrollbar?
     
    Nicholas Shewmaker, Dec 14, 2005
    #1
    1. Advertising

  2. Nicholas Shewmaker

    James Stroud Guest

    Nicholas Shewmaker wrote:
    > (I apologize if this posts twice. My AVG is being fussy.)
    >
    > From what I've read, MouseWheel is a very tricky event. I have
    > replaced my Python tcl84.dll and tk84.dll files with those in the
    > ActiveTcl distribution to fix the crashes caused by the event. Then, I
    > managed to see that my test script (see code below) was registering the
    > event by printing the delta value (+/- 120).
    >
    > -----------------
    > from Tkinter import *
    >
    > def reportScroll(event):
    > print "scrolled", event.delta, event
    >
    > root = Tk()
    >
    > myListbox = Listbox(root, selectmode=SINGLE, height=10, width=20)
    > for x in range(30):
    > myListbox.insert(END, x)
    > myListbox.pack(side=LEFT, expand=YES, fill=BOTH)
    >
    > myScrollbar = Scrollbar(root, command=myListbox.yview)
    > myScrollbar.bind('<MouseWheel>', reportScroll)
    > myScrollbar.focus_set()
    > myScrollbar.pack(side=RIGHT, fill=Y)
    >
    > myListbox.config(yscrollcommand=myScrollbar.set)
    >
    > root.mainloop()
    > -----------------
    >
    > The scrollbar and listbox are tied together, but moving the wheelmouse
    > does nothing. I realize the code above doesn't give the event any real
    > duties, but I'm not really sure how to make that leap. How and to what
    > do I bind <WheelMouse> so that it will affect the listbox and the
    > scrollbar?


    I don't think <MouseWheel> is a real event in Tkinter. Try binding to
    <Button-4> and <Button-5>:

    myListbox.bind('<Button-4>', lambda e, s=self: \
    s._scroll(SCROLL, -1, UNITS))
    myListbox.bind('<Button-5>', lambda e, s=self: \
    s._scroll(SCROLL, 1, UNITS))

    These have worked for me on Linux and OS X. They probably work under
    windows if you are lucky.

    You will need to bind the scrollbar separately if you want it to respond
    to the mousewheel.

    See also:

    http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52266

    (and the discussion that follows).

    James
     
    James Stroud, Dec 15, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ramon

    Need a faster MouseWheel

    Ramon, Dec 30, 2004, in forum: Java
    Replies:
    0
    Views:
    361
    Ramon
    Dec 30, 2004
  2. WaterWalk
    Replies:
    0
    Views:
    707
    WaterWalk
    Mar 1, 2007
  3. Richard Holmes

    Tk MouseWheel Support

    Richard Holmes, Mar 10, 2011, in forum: Python
    Replies:
    11
    Views:
    1,965
    Terry Reedy
    Mar 2, 2013
  4. MrMr[iCE]
    Replies:
    0
    Views:
    162
    MrMr[iCE]
    Aug 10, 2003
  5. David Mark

    Mousewheel event does not support DOM0 in FF

    David Mark, Jun 2, 2010, in forum: Javascript
    Replies:
    9
    Views:
    151
    David Mark
    Jun 4, 2010
Loading...

Share This Page