[Tkinter-discuss] tag_bind causes memory leak; fix?

Fredrik Lundh fredrik at pythonware.com
Sun Jun 11 11:38:12 CEST 2006


dblank at brynmawr.edu wrote:

> In order to make it so that we can click on these objects, we have code like:
> 
> self.simulator.canvas.tag_bind("robot-%s" % self.name, "<B1-Motion>",
>    func=lambda event,robot=self:self.mouse_event(event, "motion", robot))
> 
> for each object (a robot). First, is there a better way than doing this
> after every object creation?

I'd recommend using a canvas-level binding instead, and using 
find_overlapping to find the appropriate object for each mouse event.
e.g.

      def motion(event):
          widget = event.widget
          x = widget.canvasx(event.x)
          y = widget.canvasy(event.y)
          d = 5 # overlap, in canvas units
          items = widget.find_overlapping(x-d, y-d, x+d, y+d)
          if items:
              ... process items; topmost is last ...

      canvas.bind("<B1-Motion>", motion)

(I'd also recommend using a WCK view instead of the Canvas for this 
purpose, but that won't help you with the bindings issue).

</F>



More information about the Tkinter-discuss mailing list