ImageTk.Photoimage not displayed

H

harryos

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?
thanks in advance
harry
 
G

Guilherme Polo

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top