[Tutor] tkinter canvas

Alan Gauld alan.gauld at btinternet.com
Thu Jan 15 13:00:39 CET 2009

"Mr Gerard Kelly" <s4027340 at student.uq.edu.au> wrote

> All it does take a number (6 in this case) and draw that number of 
> blue
> boxes on a canvas.
> I want to be able to bind these boxes to an event - so that I can 
> either
> click on them, or hold the mouse cursor over them, and have them 
> change
> color.

I don;t think you can bind events to an object on a canvas, the events
can only be bound to the canvas.

So you would need to determine which object was being highlighted
using the x,y coordinates of the event at the canvas level and then
manipulate the shapes accordingly. This might be easier if you create
a series of shape objects and hold a list of those. You can then 
over the list to lovcate which object contains the point and that 
can update itself. This also makes handling the case of overlapping
objects easier.

There might be a more Tkinter'ish way of doing this but I've not come
across it.


Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list