5

I was wondering if there is a smart way of enabling the mouse wheel in the Tk widget 'Spinbox' in Python.

The only sensible solution I found so far was to derive a new spinbox class with the wanted functionality:

class Spinbox(tk.Spinbox):
    def __init__(self, *args, **kwargs):
        tk.Spinbox.__init__(self, *args, **kwargs)
        self.bind('<MouseWheel>', self.mouseWheel)
        self.bind('<Button-4>', self.mouseWheel)
        self.bind('<Button-5>', self.mouseWheel)

    def mouseWheel(self, event):
        if event.num == 5 or event.delta == -120:
            self.invoke('buttondown')
        elif event.num == 4 or event.delta == 120:
            self.invoke('buttonup')

But given that this would probably be the expected behavior, I am suspecting there is a better solution.

norok2
  • 25,683
  • 4
  • 73
  • 99
  • I don't know any smart way, but still, I'm just using your derived class when building the UI. – Tomasz Gandor Mar 17 '17 at 23:56
  • I am learning the hard way that `tkinter` does require some tinkering for obtaining the modern behavior of quite a few widgets. Maybe I will have time, in a not too distant future, to package my solutions in pip... – norok2 Mar 19 '17 at 19:03

1 Answers1

1

For posterity, and if you can afford it, ttk.Spinbox has mouse wheel scroll support built-in.

etauger
  • 173
  • 1
  • 6