Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How do I bind <MouseWheel> to scrollbar?

Reply
Thread Tools

How do I bind <MouseWheel> to scrollbar?

 
 
Nicholas Shewmaker
Guest
Posts: n/a
 
      12-14-2005
(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?
 
Reply With Quote
 
 
 
 
James Stroud
Guest
Posts: n/a
 
      12-15-2005
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/Coo...n/Recipe/52266

(and the discussion that follows).

James
 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
confused by boost::bind & boost::lambda::bind XHengDF@gmail.com C++ 0 05-29-2007 04:37 AM
Using a data-bind dropdownlist to populate another data-bind dropdownlist mr2_93 ASP .Net 1 10-02-2005 05:07 PM
Cannot bind to tcp/ip =?Utf-8?B?Um9zbWFuIENvbXB1dGluZw==?= Wireless Networking 1 09-23-2005 11:31 AM
How to bind data from XQuery to a Dataset ?? HNguyen ASP .Net 0 06-03-2004 07:32 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57