Tk MouseWheel Support

R

Richard Holmes

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
 
C

Corey Richardson

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.
 
M

MRAB

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()
 
A

Alexander Kapps

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()
 
D

Dick Holmes

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

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()
 
G

Greg Couch

It's Button-2 rather.

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))
 
E

enidoku

that code and other mousewheel code arent working on my pc

running windows 7 32bit python 2.6 and Mouse USB

whats the solution ?
 
M

Mark Lawrence

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? :)
 
C

Chris Angelico

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
 
T

Terry Reedy

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top