Closed callzhang closed 3 years ago
Found the reason: MacOS uses Button-2
for right button. We should update the code in canvas.py
as:
# Bind events to the Canvas
self.canvas.bind('<Configure>', lambda event: self.__size_changed()) # canvas is resized
self.canvas.bind('<Button-1>', self.__left_mouse_button) # remember canvas position
self.canvas.bind('<ButtonPress-3>', self.__right_mouse_button_pressed) # remember canvas position
self.canvas.bind('<ButtonPress-2>', self.__right_mouse_button_pressed) # remember canvas position
self.canvas.bind('<ButtonRelease-3>', self.__right_mouse_button_released) # remember canvas position
self.canvas.bind('<ButtonRelease-2>', self.__right_mouse_button_released) # remember canvas position
self.canvas.bind('<B3-Motion>', self.__right_mouse_button_motion) # move canvas to the new position
self.canvas.bind('<B2-Motion>', self.__right_mouse_button_motion) # move canvas to the new position
self.canvas.bind('<MouseWheel>', self.__wheel) # zoom for Windows and MacOS, but not Linux
self.canvas.bind('<Button-5>', self.__wheel) # zoom for Linux, wheel scroll down
self.canvas.bind('<Button-4>', self.__wheel) # zoom for Linux, wheel scroll up
Also, for mouse wheel event to be compatible with MacOS, modify as follows:
def __wheel(self, event):
""" Zoom with mouse wheel """
x = self.canvas.canvasx(event.x) # get coordinates of the event on the canvas
y = self.canvas.canvasy(event.y)
if self.outside(x, y): return # zoom only inside image area
scale = 1.0
# Respond to Linux (event.num) or Windows (event.delta) wheel event
if event.num == 5 or event.delta == -120 or event.delta == 1: # scroll down, zoom out, smaller
scale /= self.__delta
if event.num == 4 or event.delta == 120 or event.delta == -1: # scroll up, zoom in, bigger
scale *= self.__delta
self._change_canvas_scale(scale, x, y)
self.__show_image()
It works for the Mac mouse. Hope it helps.
@callzhang Thanks for your help, I've updated the code to support MacOS.
@callzhang could you tell how you installed this amazing lib on mac? I always face errors so I guess I did smth wrong
Also drag with right button is not working. Using Python 3.8 and tcl-tk 8.6.11_1 installed from brew