Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Tk MouseWheel Support

Reply
Thread Tools

Tk MouseWheel Support

 
 
Richard Holmes
Guest
Posts: n/a
 
      03-10-2011
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

 
Reply With Quote
 
 
 
 
Corey Richardson
Guest
Posts: n/a
 
      03-10-2011
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
 
Reply With Quote
 
 
 
 
Corey Richardson
Guest
Posts: n/a
 
      03-10-2011
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
 
Reply With Quote
 
MRAB
Guest
Posts: n/a
 
      03-10-2011
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-deve...on/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()
 
Reply With Quote
 
Alexander Kapps
Guest
Posts: n/a
 
      03-10-2011
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()
 
Reply With Quote
 
Dick Holmes
Guest
Posts: n/a
 
      03-11-2011
On Thu, 10 Mar 2011 21:56:52 +0100, Alexander Kapps
<(E-Mail Removed)> 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()

 
Reply With Quote
 
Greg Couch
Guest
Posts: n/a
 
      03-15-2011
On Mar 10, 1:37*pm, Corey Richardson <(E-Mail Removed)> 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))
 
Reply With Quote
 
enidoku@gmail.com
Guest
Posts: n/a
 
      03-02-2013
that code and other mousewheel code arent working on my pc

running windows 7 32bit python 2.6 and Mouse USB

whats the solution ?
 
Reply With Quote
 
Mark Lawrence
Guest
Posts: n/a
 
      03-02-2013
On 02/03/2013 21:47, http://www.velocityreviews.com/forums/(E-Mail Removed) 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

 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      03-02-2013
On Sun, Mar 3, 2013 at 10:01 AM, Mark Lawrence <(E-Mail Removed)> wrote:
> On 02/03/2013 21:47, (E-Mail Removed) 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Mousewheel event does not support DOM0 in FF David Mark Javascript 9 06-04-2010 01:24 AM
MouseWheel/IE annoyance Terry Olsen Computer Support 0 05-30-2007 01:34 AM
Make Tkinter canvas respond to MouseWheel event WaterWalk Python 0 03-01-2007 02:08 AM
How do I bind <MouseWheel> to scrollbar? Nicholas Shewmaker Python 1 12-15-2005 05:18 AM
Need a faster MouseWheel Ramon Java 0 12-30-2004 01:57 AM



Advertisments