FAQ
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

Search Discussions

  • Corey Richardson at Mar 10, 2011 at 8:35 pm

    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 at Mar 10, 2011 at 8:37 pm

    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
  • Greg Couch at Mar 15, 2011 at 8:12 pm

    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))
  • MRAB at Mar 10, 2011 at 8:51 pm

    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()
  • Alexander Kapps at Mar 10, 2011 at 8:56 pm

    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()
  • Dick Holmes at Mar 11, 2011 at 7:49 pm

    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@,
    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()

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedMar 10, '11 at 8:28p
activeMar 15, '11 at 8:12p
posts7
users5
websitepython.org

People

Translate

site design / logo © 2022 Grokbase