ImageTk.Photoimage not displayed

Guilherme Polo ggpolo at gmail.com
Thu Aug 28 15:17:16 EDT 2008


On Thu, Aug 28, 2008 at 4:07 PM, harryos <oswald.harry at gmail.com> wrote:
> hi
> i am trying to display an image on a canvas in a gui made with Tkinter
> widgets
>
>
> class PhotoDisplay:
>    def __init__(self,parent):
>        self.mainframe = Frame(parent,background="grey")
>        .....
>        #added a subframe to hold canvas and button
>        ....
>       self.canvFrame=Frame(self.mainframe,...)
>       ....
>       self.mycanvas=Canvas(self.canvFrame,...)
>       ...
>
>    def okbuttonClick(self):
>        self.mycanvas.delete(ALL)
>        myimagename=...#get an imagefilename from somewhere..
>        self.showSelectedImage(myimagename)
>
>    def showSelectedImage(self,imageName):
>        myimg=ImageTk.PhotoImage(file=imageName)
>        imgtag=self.mycanvas.create_image(70,100,image=myimg)
>        self.mycanvas.update_idletasks()
>
>
> when i click the button ,the image is displayed for a fraction of a
> second on the canvas and disappears.I couldn't figure out why this is
> happening.I am quite new to Tkinter and still going  thru Fredrik
> Lundh's intro to tkinter..can someone tell me if i am doing sthing
> wrong here?

You have to keep a reference to these images you are creating,
otherwise as soon as those methods finishes they are gone.

> thanks in advance
> harry
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
-- Guilherme H. Polo Goncalves



More information about the Python-list mailing list