Trouble displaying image with tkinter
Rob Williscroft
rtw at freenet.co.uk
Sun Aug 6 19:08:02 EDT 2006
sj wrote in news:CWfBg.7694$gY6.5173 at newssvr11.news.prodigy.com 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/
More information about the Python-list
mailing list