473,385 Members | 1,320 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How do I bind <MouseWheel> to scrollbar?

(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?
Dec 14 '05 #1
1 11139
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
Dec 15 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Gary Richardson | last post by:
I've just switched to Python 2.3.2 and have encountered a problem that causes a program to crash. The program was working with Python 2.2. The code below will illustrate the problem. A listbox is...
2
by: Buzzy | last post by:
Hi everyone, The question is below : <br> <div style="position: absolute; width: 500px; height: 100px; overflow: auto; border: 1px solid blue;"> I <b>can</b> include there some HTML...
0
by: WaterWalk | last post by:
Hello. When I tried to make Tkinter canvas widget respond to MouseWheel event on Windows XP, I failed. The canvas just doesn't receive MouseWheel event. I used bind_all to find out which widget...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.