tkInter scoping problem ?
Fredrik Lundh
fredrik at pythonware.com
Fri Oct 8 10:18:28 EDT 1999
bernard <bh at cellware.de> wrote:
> To display an image on a canvas I tried
>
> PhotoImage(name=name, file="./" + name + ".gif")
> c.create_image(x, y, anchor=NW, image=name)
>
> which works fine.
now that's an interesting way to abuse Tkinter objects :-)
> However when I use this code in the __init__ method of a class
>
> class TeleTubby:
> def __init__(self, c, name, x, y):
> PhotoImage(name=name, file="./" + name + ".gif")
> c.create_image(x, y, anchor=NW, image=name)
> ...
>
> I get the error
> tcl Error: image "tinky" doesn't exist
if you don't hold on to the PhotoImage object, it's
garbage collected by Python before the call to
create_image.
(if you run the same code in the interpreter, the magic
"_" (last result) variable keeps a reference to the image,
for a short while -- when "_" is used for the next result,
the image object is garbage collected, and the image
shown on the canvas is blanked...)
> Then trying
>
> self.pic = PhotoImage(file="./" + name + ".gif")
> c.create_image(x, y, anchor=NW, self.pic)
>
> this time I get no error but no image.
try this:
self.pic = PhotoImage(file="./" + name + ".gif")
c.create_image(x, y, anchor=NW, image=self.pic)
</F>
More information about the Python-list
mailing list