TK binding mouse wheel scroll to scrolled list

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    TK binding mouse wheel scroll to scrolled list

    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:
    [code=python]
    class ScrolledList(Fr ame):
    def __init__(self, parent = None):
    Frame.__init__( self, parent)
    self.box = Listbox(self)
    ... more code ...

    self.box.bind(" <MouseWheel>" , self.wheelscrol l)

    def wheelscroll(sel f, event):
    if event.delta > 0:
    self.box.yview_ scroll(-2, 'units')
    else:
    self.box.yview_ scroll(2, 'units')
    [/code]
    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?
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by Michael
    Hi John,

    here is a code snippet I have successfully used:

    self.canvas.bin d('<4>', lambda event : self.canvas.xvi ew('scroll', -1, 'units'))
    self.canvas.bin d('<5>', lambda event : self.canvas.xvi ew('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.xvi ew('scroll', -1, 'units')
    elif event.num == 5:
    self.canvas.xvi ew('scroll', 1, 'units')

    I hope this helps
    From mail.python.org/pipemail, June of '06.

    Hope that helps.

    Comment

    • ilikepython
      Recognized Expert Contributor
      • Feb 2007
      • 844

      #3
      Originally posted by bartonc
      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.

      Comment

      • ilikepython
        Recognized Expert Contributor
        • Feb 2007
        • 844

        #4
        I figured it out. The problem was that the list box never got focus. I added this line and it worked:
        [code=python]
        self.box.bind(" <Button-1>", lambda x: self.box.focus( ))
        [/code]

        Comment

        Working...