Tk MouseWheel Support

Discussion in 'Python' started by Richard Holmes, Mar 10, 2011.

  1. I am trying to use the mouse wheel to scroll a list box, but I'm not
    getting the event. When I bind "<Button-1>" to the listbox I get the
    event and I'm able to scroll using yview_scroll. I've tried binding
    "MouseWheel" and "<MouseWheel>", and I've also tried "<Button-4>" and
    "<Button-5>" even though I'm using Windows (XP, if that makes a
    difference). None of these works (I'm using print to see if I got to
    an event handler, and there's no printout).

    TIA for any help!

    Dick
    Richard Holmes, Mar 10, 2011
    #1
    1. Advertising

  2. On 03/10/2011 03:28 PM, Richard Holmes wrote:
    > I am trying to use the mouse wheel to scroll a list box, but I'm not
    > getting the event. When I bind "<Button-1>" to the listbox I get the
    > event and I'm able to scroll using yview_scroll. I've tried binding
    > "MouseWheel" and "<MouseWheel>", and I've also tried "<Button-4>" and
    > "<Button-5>" even though I'm using Windows (XP, if that makes a
    > difference). None of these works (I'm using print to see if I got to
    > an event handler, and there's no printout).
    >
    > TIA for any help!
    >
    > Dick


    Middle button is Button-3 IIRC, and I think it'd be MouseUp and
    MouseDown. I'm really not sure though.


    --
    Corey Richardson
    Corey Richardson, Mar 10, 2011
    #2
    1. Advertising

  3. On 03/10/2011 03:35 PM, Corey Richardson wrote:
    > Middle button is Button-3 IIRC, and I think it'd be MouseUp and
    > MouseDown. I'm really not sure though.


    It's Button-2 rather.

    --
    Corey Richardson
    Corey Richardson, Mar 10, 2011
    #3
  4. Richard Holmes

    MRAB Guest

    On 10/03/2011 20:28, Richard Holmes wrote:
    > I am trying to use the mouse wheel to scroll a list box, but I'm not
    > getting the event. When I bind "<Button-1>" to the listbox I get the
    > event and I'm able to scroll using yview_scroll. I've tried binding
    > "MouseWheel" and"<MouseWheel>", and I've also tried"<Button-4>" and
    > "<Button-5>" even though I'm using Windows (XP, if that makes a
    > difference). None of these works (I'm using print to see if I got to
    > an event handler, and there's no printout).
    >
    > TIA for any help!
    >
    > Dick
    >

    Google found this:

    Using the Mouse Wheel with Tkinter (Python)
    http://www.daniweb.com/software-development/python/code/217059

    which I adapted to get this (tested on XP with Python 2.7):

    import Tkinter as tk

    def mouse_wheel(event):
    dir = 0
    # respond to Linux or Windows wheel event
    if event.num == 5 or event.delta == -120:
    dir = 1
    if event.num == 4 or event.delta == 120:
    dir = -1
    listbox.yview_scroll(dir, "units")

    root = tk.Tk()
    root.title('turn mouse wheel')
    root['bg'] = 'darkgreen'

    # with Windows OS
    root.bind("<MouseWheel>", mouse_wheel)
    # with Linux OS
    root.bind("<Button-4>", mouse_wheel)
    root.bind("<Button-5>", mouse_wheel)

    listbox = tk.Listbox(root)
    listbox.pack()
    for i in range(100):
    listbox.insert(tk.END, str(i))

    root.mainloop()
    MRAB, Mar 10, 2011
    #4
  5. On 10.03.2011 21:28, Richard Holmes wrote:
    > I am trying to use the mouse wheel to scroll a list box, but I'm not
    > getting the event. When I bind "<Button-1>" to the listbox I get the
    > event and I'm able to scroll using yview_scroll. I've tried binding
    > "MouseWheel" and"<MouseWheel>", and I've also tried"<Button-4>" and
    > "<Button-5>" even though I'm using Windows (XP, if that makes a
    > difference). None of these works (I'm using print to see if I got to
    > an event handler, and there's no printout).
    >
    > TIA for any help!
    >
    > Dick


    Can you post your code please (if it's too long, strip it down to
    the smallest program which still shows the problem.)

    On Ubuntu 10.04, Python 2.6.5, the Listbox already recognizes the
    mouse wheel. Listbox aside, the following works here:


    import Tkinter as tk

    def wheel_up(event):
    print "wheel_up", event

    def wheel_down(event):
    print "wheel_down", event

    root = tk.Tk()
    root.bind("<Button-4>", wheel_up)
    root.bind("<Button-5>", wheel_down)
    root.mainloop()
    Alexander Kapps, Mar 10, 2011
    #5
  6. Richard Holmes

    Dick Holmes Guest

    On Thu, 10 Mar 2011 21:56:52 +0100, Alexander Kapps
    <> wrote:

    <snip>
    >Can you post your code please (if it's too long, strip it down to
    >the smallest program which still shows the problem.)


    First, thanks to MRAB for showing me how to get the wheel working.

    In the following code (Windows only), rolling the wheel doesn't invoke
    the event method. If I change the mouse wheel binding to
    self.master.bind... the event gets called and the scrolling works.
    Note that the scrolling occurs even when the mouse is outside the
    listbox. I guess I'll have to look at the mouse position when I enter
    the wheel_event method to see if the mouse is over the listbox.

    # mouse wheel in listbox
    import Tkinter as tk

    class Application(tk.Frame):
    def __init__(self, master=None):
    tk.Frame.__init__(self, master)
    self.grid()

    self.button = tk.Button(self, text='Button')
    self.button.grid(row=0, column=0)

    self.yscroll = tk.Scrollbar(self, orient=tk.VERTICAL)
    self.yscroll.grid(row=1, column=1, sticky=tk.N+tk.S)
    self.list = tk.Listbox(self, selectmode=tk.SINGLE, width=40,
    height=5,
    yscrollcommand=self.yscroll.set)
    self.list.grid(row=1, column=0, sticky=tk.N+tk.S+tk.E+tk.W)
    self.yscroll["command"] = self.list.yview
    self.list.bind("<MouseWheel>", self.wheel_event)
    for i in xrange(10):
    self.list.insert(tk.END, 'line ' + str(i))

    def wheel_event(self, evt):
    lines = evt.delta // 120
    self.list.yview_scroll(-lines, tk.UNITS)

    root = tk.Tk()
    root.geometry('300x300+400+200')
    root.title(' Wheel Test')
    app = Application(master = root)
    root.mainloop()
    Dick Holmes, Mar 11, 2011
    #6
  7. Richard Holmes

    Greg Couch Guest

    On Mar 10, 1:37 pm, Corey Richardson <> wrote:
    > On 03/10/2011 03:35 PM, Corey Richardson wrote:
    >
    > > Middle button is Button-3 IIRC, and I think it'd be MouseUp and
    > > MouseDown. I'm really not sure though.

    >
    > It's Button-2 rather.
    >
    > --
    > Corey Richardson


    Middle button is button-3 on Mac OS X, and button-2 on Linux and
    Windows. Very annoying, to say the least.

    For X11, here's a better way to inject MouseWheel events so all
    widgets that understand MouseWheel events will work without additional
    code:

    def mousewheel(e, delta):
    try:
    w = e.widget.focus_displayof()
    except AttributeError:
    # e.widget might be string
    # if not created by Tkinter (eg., menu tearoff)
    return
    if w: w.event_generate('<MouseWheel>', delta=delta,
    state=e.state, rootx=e.x_root, rooty=e.y_root,
    x=e.x, y=e.y, time=e.time)
    root.bind_all('<Button-4>', lambda e, mw=mousewheel: mw(e, 120))
    root.bind_all('<Button-5>', lambda e, mw=mousewheel: mw(e, -120))
    Greg Couch, Mar 15, 2011
    #7
  8. Richard Holmes

    Guest

    that code and other mousewheel code arent working on my pc

    running windows 7 32bit python 2.6 and Mouse USB

    whats the solution ?
    , Mar 2, 2013
    #8
  9. On 02/03/2013 21:47, wrote:
    > that code and other mousewheel code arent working on my pc
    >
    > running windows 7 32bit python 2.6 and Mouse USB
    >
    > whats the solution ?
    >


    Post something that we can read, or do I give up on Thunderbird? :)

    --
    Cheers.

    Mark Lawrence
    Mark Lawrence, Mar 2, 2013
    #9
  10. On Sun, Mar 3, 2013 at 10:01 AM, Mark Lawrence <> wrote:
    > On 02/03/2013 21:47, wrote:
    >>
    >> that code and other mousewheel code arent working on my pc
    >>
    >> running windows 7 32bit python 2.6 and Mouse USB
    >>
    >> whats the solution ?
    >>

    >
    > Post something that we can read, or do I give up on Thunderbird? :)


    I don't think Thunderbird's at fault here, unless it damaged the
    original post itself. Is that a known bug? "Reading posts in
    Thunderbird while the moon is in its first quarter and pigs are flying
    overhead causes the OP to lose 40 points of apparent IQ"?

    ChrisA
    Chris Angelico, Mar 2, 2013
    #10
  11. Richard Holmes

    Dave Angel Guest

    On 03/02/2013 06:01 PM, Mark Lawrence wrote:
    > On 02/03/2013 21:47, wrote:
    >> that code and other mousewheel code arent working on my pc
    >>
    >> running windows 7 32bit python 2.6 and Mouse USB
    >>
    >> whats the solution ?
    >>

    >
    > Post something that we can read, or do I give up on Thunderbird? :)
    >


    That enidoku person was replying to a two-year old thread. If you want
    to see the thread, check out:

    http://www.velocityreviews.com/forums/t744913-tk-mousewheel-support.html

    --
    DaveA
    Dave Angel, Mar 2, 2013
    #11
  12. Richard Holmes

    Terry Reedy Guest

    On 3/2/2013 6:01 PM, Mark Lawrence wrote:
    > On 02/03/2013 21:47, wrote:
    >> that code and other mousewheel code arent working on my pc
    >> running windows 7 32bit python 2.6 and Mouse USB
    >> whats the solution ?


    > Post something that we can read, or do I give up on Thunderbird? :)


    In tbird, temporarily switching from view/threads/unread to
    view/threads/all will expose older posts retained on your system, but
    enidoku appears to be replying to a post more than 180 days old (my
    retention period for this group). So more info is needed.

    --
    Terry Jan Reedy
    Terry Reedy, Mar 2, 2013
    #12
    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:
    358
    Ramon
    Dec 30, 2004
  2. Nicholas Shewmaker

    How do I bind <MouseWheel> to scrollbar?

    Nicholas Shewmaker, Dec 14, 2005, in forum: Python
    Replies:
    1
    Views:
    501
    James Stroud
    Dec 15, 2005
  3. WaterWalk
    Replies:
    0
    Views:
    692
    WaterWalk
    Mar 1, 2007
  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:
    147
    David Mark
    Jun 4, 2010
Loading...

Share This Page