Skip to content
Advertisement

Suppressing other canvas items from reacting to event

I have a rectangle on a canvas listening for events, with a callback function “A”.

The blank space in the canvas is also listening to events with a callback function “B”.

When the I right-click on the rectangle, both “A” and “B” are executed. I understand why this happens (the rectangle is on the canvas).

The behavior I want to create is execution of “A” only when I right-click on the rectangle. How can I stop the empty canvas area from reacting to the event?

See the code below as an example of the behavior I currently have.

JavaScript

Advertisement

Answer

There is nothing built into tkinter that does this. In fact, the Canvas documentation specifically calls this out:

If bindings have been created for a canvas window using the bind command, then they are invoked in addition to bindings created for the canvas’s items using the bind widget command. The bindings for items will be invoked before any of the bindings for the window as a whole.

One workaround is to have your tag binding set some sort of flag to tell the canvas binding not to do any work.

Example:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement