jupyter-widgets-contrib/ipycanvas

Mouse Events with Multicanvas not working

Closed this issue · 4 comments

Hi,

If I create an object multi_canvas of class Multicanvas() and try:

multi_canvas.on_mouse_down(handle_mouse_down)

The error is raised:

AttributeError: 'super' object has no attribute '__getattr__'

I assumed that is because on_mouse_down function is implemented only in the regular Canvas() class.

Is there any way to get mouse-click events with multicanvas?

Thanks a lot!

Thanks for opening an issue! This is an odd error message :S would you be able to provide a simple code sample that replicates this issue?

That is all I did so far:

vbox = VBox()
vbox.layout.width = "100%"

multi_canvas = MultiCanvas(3, width=width, height=height)
multi_canvas[0].draw_image(image, 0, 0, width=width, height=height) 
multi_canvas[1].draw_image(boundary_image, 0, 0, width=width, height=height) 
multi_canvas[2].clear_rect(0, 0, width=width, height=height)

coordinates = HTML('<h3>Click an image to see the click coordinates here</h3>')

vbox.children = [multi_canvas, coordinates]

def handle_mouse_down(x, y):
    coordinates.value = '<h3>Clicked at ({}, {})</h3>'.format(x, y)

multi_canvas.on_mouse_down(handle_mouse_down)

vbox

Here image, boundary_image are just objects of Image from regular ipywidgets. The same code works perfectly fine if I use Canvas instead of MultiCanvas.

I hope that helps!

Thanks! I am able to reproduce. This is definitely a bug, I'll try to push a fix for the next ipycanvas version. In the meantime, I'd suggest hooking the event listener on the top canvas:

vbox = VBox()
vbox.layout.width = "100%"

multi_canvas = MultiCanvas(3, width=width, height=height)
multi_canvas[0].draw_image(image, 0, 0, width=width, height=height) 
multi_canvas[1].draw_image(boundary_image, 0, 0, width=width, height=height) 
multi_canvas[2].clear_rect(0, 0, width=width, height=height)

coordinates = HTML('<h3>Click an image to see the click coordinates here</h3>')

vbox.children = [multi_canvas, coordinates]

def handle_mouse_down(x, y):
    coordinates.value = '<h3>Clicked at ({}, {})</h3>'.format(x, y)

# DO THIS INSTEAD
multi_canvas[2].on_mouse_down(handle_mouse_down)
#

vbox

Thank you very much for your help! It works for me now. :)