Grasshopper
Grasshopper

Reputation: 436

MouseWheel up and down bindings not working in tkinter

I am working with the following code -

self.table.Table.bind("<MouseWheel-Down>", lambda x: self.table.shiftTable('right'))
self.table.Table.bind('<MouseWheel-Up>', lambda x: self.table.shiftTable('left'))          #mousewheel up and down not working for unknown reasons
self.table.Table.bind('<KeyPress-Left>', lambda x: self.table.shiftTable('left'))
self.table.Table.bind('<KeyPress-Right>', lambda x: self.table.shiftTable('right'))
self.table.Table.bind('<Enter>', lambda x: self.table.Table.focus())
self.table.Table.bind('<Leave>', lambda x: self.mainUI_object.fg_root.focus())

The problem here is that the mousewheel bindings are not working though not giving errors. On the other hand the following code piece works (note the absence of up and down specifiers) -

self.table.Table.bind("<MouseWheel>", lambda x: self.table.shiftTable('right'))

What can be the possible reasons for not working and what is the solution to get the up and down work?

Upvotes: 2

Views: 1799

Answers (1)

j_4321
j_4321

Reputation: 16169

In Windows and Mac, <MouseWheel-Down/Up> is not a binding to turning the mouse wheel up or down, instead it corresponds to simultaneously using the mouse wheel and pressing the Down/Up arrow key. The MouseWheel binding cannot be made direction specific, however the direction of the scrolling is contained in event.delta. This means you can do something like

def mouse_wheel_binding(self, event):
    if event.delta > 0: 
        self.table.shiftTable('right')
    else:
        self.table.shiftTable('left')

and

self.table.Table.bind("<MouseWheel>", self.mouse_wheel_binding)

I don't have Windows or Mac so I don't know which sign correspond to Up/Down, so you might have to switch the left/right.

Note: In Linux, the mouse wheel bindings are direction specific: MouseWheel-Up is '<Button-4>' and MouseWheel-Down is '<Button-5>'and the "<MouseWheel>" binding does not work.

Upvotes: 7

Related Questions