[Python-de] RE: [Tutor] Images + Tkinter

alan.gauld at bt.com alan.gauld at bt.com
Sun Nov 17 17:50:03 EST 2002


> Martin v. Loewis has sent me the following explanation:

> When you drop the last reference to an Tkinter.Image object, the image
> is destroyed. So always keep a reference to an image that you 
> want to use.

I feel stupid. I must be missing something...

> def window(tk):
>     global photo       #### SIC!
>     frame=Tkinter.Frame(tk)
>     frame.pack()
>     canvas=Tkinter.Canvas(frame,width=400,height=500)
>     canvas.pack()
>     photo=Tkinter.PhotoImage(file="picture.gif")
>     canvas.create_rectangle(10,20,30,40, fill="red")
>     canvas.create_image(200, 250, image=photo)

Why doesn't this retain a reference to the image and stop it 
being dropped?

>     button=Tkinter.Button(frame, text="EXIT", command=tk.destroy)
>     button.pack()

In fact all of these objects should go out of scope at the end of 
the function except for the fact they are parented on tk which is 
global. So why does the image die but not the button?

Still confused.

Alan G.




More information about the Python-de mailing list