Tkinter: memory loss for tag bindings in Canvas

Eric Brunel eric.brunel at pragmadev.com
Wed Mar 13 09:24:10 EST 2002


Hi all,

We've noticed a memory problem when doing multiple item deletion, 
(re)creations and tag bindings in Tkinter canvases. When monitoring the 
occupied memory of the following code:

--------------------
from Tkinter import *

root = Tk()
c = Canvas(root)
c.pack(side=TOP)

def p(event): print 'beans'

def foo():
  for i in range(1000):
    c.delete(ALL)
    tId = c.create_text(100, 100, text='spam')
    c.tag_bind(tId, "<Button-1>", p)

Button(root, text="Go", command=foo).pack(side=BOTTOM)

root.mainloop()
--------------------

we saw that every time we clicked on the "Go" button, the memory taken by 
the program significantly increased. Quite obviously, it shouldn't: the end 
state is exactly the same than the start state. Removing the "c.tag_bind" 
line solves the problem. This happens on Win2K, Linux (Mandrake 8.0) and 
Solaris 2.7.

The same program written in tcl/tk doesn't result in memory loss, and 
deleting the bindings with

    for item in c.find_all():
      for seq in c.tag_bind(item):
        c.tag_unbind(item, seq)

at the beginning of the "foo" function doesn't help.

I've looked into the Tkinter module and found that a new tcl command is 
being created for each binding. My guess is that for some reason, this 
command doesn't get properly deleted when the item is deleted or the 
binding destroyed.

Does anybody has a solution or workaround for this problem?

TIA
 - eric -




More information about the Python-list mailing list