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

ilikepython
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.         self.box = Listbox(self)
  5.         ... more code ...
  6.  
  7.         self.box.bind("<MouseWheel>", self.wheelscroll)
  8.  
  9.     def wheelscroll(self, event):
  10.         if event.delta > 0:
  11.             self.box.yview_scroll(-2, 'units')
  12.         else:
  13.             self.box.yview_scroll(2, 'units')
  14.  
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

bartonc
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 mail.python.org/pipemail, June of '06.

Hope that helps.
Aug 21 '07 #2

ilikepython
Expert 100+
P: 844
From mail.python.org/pipemail, 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

ilikepython
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. self.box.bind("<Button-1>", lambda x: self.box.focus())
  2.  
Aug 22 '07 #4

Post your reply

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