469,588 Members | 2,741 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,588 developers. It's quick & easy.

TK binding mouse wheel scroll to scrolled list

ilikepython
844 Expert 512MB
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
3 4663
bartonc
6,596 Expert 4TB
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
844 Expert 512MB
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
844 Expert 512MB
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.

Similar topics

reply views Thread by Jack | last post: by
1 post views Thread by Marcin | last post: by
7 posts views Thread by tommaso.gastaldi | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.