Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Trouble displaying image with tkinter

Reply
Thread Tools

Trouble displaying image with tkinter

 
 
sj
Guest
Posts: n/a
 
      08-06-2006
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()
 
Reply With Quote
 
 
 
 
jmdeschamps@gmail.com
Guest
Posts: n/a
 
      08-06-2006

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

 
Reply With Quote
 
 
 
 
Rob Williscroft
Guest
Posts: n/a
 
      08-06-2006
sj wrote in news:CWfBg.7694$(E-Mail Removed) 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/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
trouble displaying image stored in database Jeff ASP .Net 6 10-02-2008 07:59 PM
Again, Downloading and Displaying an Image from the Internet in Tkinter Dustan Python 6 06-07-2006 12:37 PM
Downloading and Displaying an Image from the Internet in Tkinter Dustan Python 0 06-01-2006 01:12 AM
Displaying image after selecting an image file. http://www.visual-basic-data-mining.net/forum ASP .Net 0 09-15-2004 01:36 PM
Re: displaying video in a tkinter GUI Jim Easterbrook Python 0 08-22-2003 08:38 AM



Advertisments