Problem with Tkinter.PhotoImage

Achim Gaedke Achim.Gaedke at zpr.uni-koeln.de
Tue Feb 20 07:25:28 EST 2001


Hello everybody!

I've tested some features of the Tkinter.Canvas and found an odd
behaviour.

Here is the example that works:

import Tkinter

r=Tkinter.Tk()
c=Tkinter.Canvas(r)
p=Tkinter.PhotoImage(file='linuxorg.gif')
i=c.create_image(0,0,anchor=Tkinter.NW,image=p)
c.pack()

r.mainloop()

And now a simple example that does not show the image and does not
generate runtime errors.

import Tkinter

r=Tkinter.Tk()
c=Tkinter.Canvas(r)
i=c.create_image(0,0,anchor=Tkinter.NW,
                 image=Tkinter.PhotoImage(file='linuxorg.gif'))
c.pack()

r.mainloop()

This example is the simplest. Here is a more elaborate case (my original
problem):

import Tkinter

class picture:

    def __init__(self,canvas):
        self.p=Tkinter.PhotoImage(file='linuxorg.gif')
        self.i=c.create_image(0,0,anchor=Tkinter.NW,
                              image=self.p)


r=Tkinter.Tk()
c=Tkinter.Canvas(r)
picture(c)
c.pack()

r.mainloop()

This doesn't work. Just change the line

picture(c)

to

p=picture(c).

this reveals the reason of the strange behaviour. There is no reference
left on Tkinter.PhotoImage, so it is destructed.
But I do not think, that it should be, just because Phython is an "easy
going" language with reference counters and should keep one reference,
although this is held by the canvas-item image.

Does someone have the same problem with this behaviour?

Yours

Achim





More information about the Python-list mailing list