By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,603 Members | 975 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,603 IT Pros & Developers. It's quick & easy.

TK binding mouse wheel scroll to scrolled list

Expert 100+
P: 844
I have a class that I use for a scrolled list. The problem is I want to be able to scroll the list using the mouse wheel. I am using bind and it's not working:
Expand|Select|Wrap|Line Numbers
  1. class ScrolledList(Frame):
  2.     def __init__(self, parent = None):
  3.         Frame.__init__(self, parent)
  4. = Listbox(self)
  5.         ... more code ...
  7."<MouseWheel>", self.wheelscroll)
  9.     def wheelscroll(self, event):
  10.         if > 0:
  11.   , 'units')
  12.         else:
  13.   , 'units')
I also tried binding to the Frame but that doesn't work. I think the problem is the box isn't getting the messages, but then what should I bind to? Also, If I don't bind to the box, what happens if I have more than one scrolled list in one frame?
Aug 21 '07 #1
Share this Question
Share on Google+
3 Replies

Expert 5K+
P: 6,596
Hi John,

here is a code snippet I have successfully used:

self.canvas.bind('<4>', lambda event : self.canvas.xview('scroll', -1, 'units'))
self.canvas.bind('<5>', lambda event : self.canvas.xview('scroll', 1, 'units'))

or if you do not like the lambda, you can use a callback like this:

def rollWheel(event):
if event.num == 4:
self.canvas.xview('scroll', -1, 'units')
elif event.num == 5:
self.canvas.xview('scroll', 1, 'units')

I hope this helps
From, June of '06.

Hope that helps.
Aug 21 '07 #2

Expert 100+
P: 844
From, June of '06.

Hope that helps.
Well I found that same page too, but it turns out that button 4 and 5 work only on linux, and on windows you have to use the mousewheel event.
Aug 21 '07 #3

Expert 100+
P: 844
I figured it out. The problem was that the list box never got focus. I added this line and it worked:
Expand|Select|Wrap|Line Numbers
  1."<Button-1>", lambda x:
Aug 22 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.