List Info

Thread: tag_bind causes memory leak; fix?




tag_bind causes memory leak; fix?
user name
2006-06-10 21:50:54
Tkinter users,

I have a Tkinter app that redraws objects on a canvas, about
10 times a
second. It doesn't reuse the drawn objects, it just draws
and deletes.

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?

One problem with this method is that it appears to cause a
memory leak.
Trying to unbind these, like:

self.simulator.canvas.unbind_all("<B1-Motion>&q
uot;)

or this method:

self.simulator.canvas.tag_unbind("robot-%s" %
self.name, "<B1-Motion>")

doesn't seem to help. Any ideas on a better way, or how to
stop the memory
leak?

Thanks!

-Doug

_______________________________________________
Tkinter-discuss mailing list
Tkinter-discusspython.org
http://mail.python.org/mailman/listinfo/tkinter-discuss
tag_bind causes memory leak; fix?
user name
2006-06-11 09:38:12
dblankbrynmawr.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>

_______________________________________________
Tkinter-discuss mailing list
Tkinter-discusspython.org
http://mail.python.org/mailman/listinfo/tkinter-discuss
[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )