469,646 Members | 1,395 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,646 developers. It's quick & easy.

Trouble displaying image with tkinter

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()
Aug 6 '06 #1
2 8341

sj wrote:
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

Aug 6 '06 #2
sj wrote in news:CW*****************@newssvr11.news.prodigy.co m 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.
--
http://www.victim-prime.dsl.pipex.com/
Aug 6 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Rick Olson | last post: by
3 posts views Thread by Terry Carroll | last post: by
5 posts views Thread by exhuma.twn | last post: by
1 post views Thread by wilson | last post: by
4 posts views Thread by skanemupp | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.