[Image-SIG] Image in Text

kevin at cazabon.com kevin at cazabon.com
Sat Nov 20 17:04:32 CET 2004


actually, yes it does display.  However, as soon as you exit the scope of
the function you defined (sub), the photo gets garbage collected.

So, you have to make a permanent reference to the photo to keep it around,
here's a simple hack:

def sub(root):
    text = Text(root)
    text.insert(END, "here's my photo")
    root.photo=PhotoImage(file='lumber.gif')
    text.image_create(END, image=root.photo)
    text.pack()


----- Original Message ----- 
From: "William Baxter" <bbaxter at wadsworth.org>
To: "Fredrik Lundh" <fredrik at pythonware.com>; <image-sig at python.org>
Sent: Friday, November 19, 2004 8:53 PM
Subject: [Image-SIG] Image in Text


> Hello,
>
> How come my image displays in this Text box,
>
> from Tkinter import *
> root = Tk()
>
> text = Text(root)
> text.insert(END, "here's my photo")
> photo=PhotoImage(file='lumber.gif')
> text.image_create(END, image=photo)
> text.pack()
>
> root.mainloop()
>
> ==========================
>
> but not in this one (inside a function, or a class)?
>
> from Tkinter import *
> root = Tk()
> def sub(root):
>     text = Text(root)
>     text.insert(END, "here's my photo")
>     photo=PhotoImage(file='lumber.gif')
>     text.image_create(END, image=photo)
>     text.pack()
>
> sub(root)
> root.mainloop()
>
> -- 
> Thanks,
> Bill B.
>
> ----
> William T. Baxter, Ph.D.
> Wadsworth Center
> Empire State Plaza, PO Box 509
> Albany, NY 12201-0509


----------------------------------------------------------------------------
----


> from Tkinter import *
>
> root = Tk()
>
> text = Text(root)
> text.insert(END, "here's my photo")
> photo=PhotoImage(file='lumber.gif')
> text.image_create(END, image=photo)
> text.pack()
>
> root.mainloop()
>
>


----------------------------------------------------------------------------
----


> from Tkinter import *
>
> root = Tk()
>
> def sub(root):
>
>     text = Text(root)
>     text.insert(END, "here's my photo")
>     photo=PhotoImage(file='lumber.gif')
>     text.image_create(END, image=photo)
>     text.pack()
>
> sub(root)
>
> root.mainloop()
>
>


----------------------------------------------------------------------------
----


> _______________________________________________
> Image-SIG maillist  -  Image-SIG at python.org
> http://mail.python.org/mailman/listinfo/image-sig
>




More information about the Image-SIG mailing list