[Tkinter-discuss] How can mouse read canvas's object's info?

John McMonagle jmcmonagle at velseis.com.au
Tue Apr 15 01:10:10 CEST 2008

chenguang Zhang wrote:
> Suppose there are several points on my canvas, each corresponds to a
> data of the form [point index, point's x coordanate, point's y
> coordinate], all these points are
> created by the following sentence,
> for point in self.data:    
> #self.data is a point list and has the form [point1, point2, ...]
>      self.drawing.create_oval(point[1]-2, point[2]-2, point[1]+2,      
> point[2]+2,fill='black',tags='Toselect')
> #self.drawing is a Canvas
>      self.drawing.tag_bind('Toselect', '<Any-Enter>', self.mouseEnter)
> and the following is the self.mouseEnter function:
> def mouseEnter(self,event):
> self.CoordinateShow.configure(text='point:'+str(event.x)+','+str(event.y))
> #self.CoordinateShow is a Label to show the point's information
> I want to show the related information of the point when my mouse moves
> upon them...
> The KEY problem is: How can the mouse read out that point's OWN
> information, such as the point's index, x and y coordinate, and even the
> point's color. You see, the mouse's function here accually works on its
> own x and y coordinates(event.x and event.y), not the point's value.
> (now the self.mouseEnter function don't manipulate color, but my future
> work will have to handle this...)

You need to convert the screen coordinate to your canvas coordinate.

Replace event.x and event.y with self.drawing.canvasx(event.x) and
self.drawing.canvasy(event.y) respectively.



More information about the Tkinter-discuss mailing list