Trouble displaying image with tkinter

S

sj

I am just learning to use Tkinter and am having problems displaying image
files. I am able to display an image using tutorials (such as
http://www.daniweb.com/code/snippet296.html) But when I try my own code all
I get is an empty widget. What is wrong with the following program?




from Tkinter import *

class Foo(Frame):

def __init__(self,master=None):
Frame.__init__(self,master)
self.pack()
self.createWidgets()



def createWidgets(self):

self.qbutton = Button(self)
self.qbutton["text"] = "Quit"
self.qbutton["command"] = self.quit
self.qbutton.pack(side = "top")

idata =
PhotoImage(file="/home/sj/documents/projects/xaed/images/cat_001.gif")

canvas = Canvas(width=300,height=200)
canvas.pack(side="top",fill=BOTH,expand=YES)
canvas.create_image(50,10,image=idata,anchor=NW)

## lab = Label(self,image=idata)
## lab.pack(side=TOP)


root = Tk()
app = Foo(root)
app.mainloop()
#app.destroy()
 
J

jmdeschamps

sj said:
I am just learning to use Tkinter and am having problems displaying image
files. I am able to display an image using tutorials (such as
http://www.daniweb.com/code/snippet296.html) But when I try my own code all
I get is an empty widget. What is wrong with the following program?



from Tkinter import *

class Foo(Frame):

def __init__(self,master=None):
Frame.__init__(self,master)
self.pack()
self.createWidgets()


def createWidgets(self):

self.qbutton = Button(self)
self.qbutton["text"] = "Quit"
self.qbutton["command"] = self.quit
self.qbutton.pack(side = "top")

idata =
PhotoImage(file="/home/sj/documents/projects/xaed/images/cat_001.gif")

canvas = Canvas(width=300,height=200)
canvas.pack(side="top",fill=BOTH,expand=YES)
canvas.create_image(50,10,image=idata,anchor=NW)

## lab = Label(self,image=idata)
## lab.pack(side=TOP)


root = Tk()
app = Foo(root)
app.mainloop()
#app.destroy()

If you keep a reference of the photoImage object then it will work!
....
self.idata=
PhotoImage(file="/home/sj/documents/projects/xaed/images/cat_001.gif")
....
canvas.create_image(50,10,image=iself.data,anchor=NW)
....
By making the PhotoImage an attribute of your object, you keep a
reference that the garbage collector will NOT collect, So you're image
will continue to exist and thus be rendered by the canvas.

JM
 
R

Rob Williscroft

sj wrote in in
comp.lang.python:
I am just learning to use Tkinter and am having problems displaying
image files. I am able to display an image using tutorials (such as
http://www.daniweb.com/code/snippet296.html) But when I try my own
code all I get is an empty widget. What is wrong with the following
program?

The problem is that CPython is (garbage) collecting the image.

The canvas object is using it (i.e. passing to TCL/TK) but not
keeping a reference to it. change idata to self.idata and all
should be well.
from Tkinter import *

class Foo(Frame):
[snip]


idata =
PhotoImage(file="/home/sj/documents/projects/xaed/images/cat_001.gif")
self.idata = PhotoImage .....
canvas = Canvas(width=300,height=200)

Your missing a parent reference in there (self in this case) i.e.:

canvas = Canvas(self, width=300,height=200)


but when I tested it it didn't seem to matter. I'd guess
however that when the layout gets more complex it will
make a difference.

Rob.
 

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
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top