By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,426 Members | 2,928 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,426 IT Pros & Developers. It's quick & easy.

Trouble displaying image with tkinter

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a

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

P: n/a
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.