Skyler Skyler - 3 months ago 42
Python Question

how to create a transparent rectangle responding to click event in Tkinter

I need to draw a rectangle in a tkinter.canvas to respond click event:

click_area = self.canvas.create_rectangle(0,0,pa_width,pa_height,fill='LightBlue',outline='lightBlue',tags=['A','CLICK_AREA'])
self.canvas.tag_bind('CLICK_AREA','<Button>',self.onClickArea)


it works.

at this moment, I have to draw a series of grid on the canvas, and I want them to be covered with the
click_area
, so that I need to make the
click_area
transparent.

but, when I wrote like this:

click_area = self.canvas.create_rectangle(0,0,pa_width,pa_height,fill='',outline='lightBlue',tags=['A','CLICK_AREA'])


it did not respond to click any longer.

So, my question is how to make it transparent and keep it responding to click. Or, is there any other way to implement what I want.

Thank you very much.

Answer

I think I got it: Bind the canvas, not the rectangle.

replace

self.canvas.tag_bind('CLICK_AREA','<Button>',self.onClickArea)

with

self.canvas.bind('<Button>',self.onClickArea)

problem solved.