Trouble displaying image with tkinter

Discussion in 'Python' started by sj, Aug 6, 2006.

  1. sj

    sj Guest

    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()
     
    sj, Aug 6, 2006
    #1
    1. Advertising

  2. sj

    Guest

    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, 2006
    #2
    1. Advertising

  3. sj wrote in news:CWfBg.7694$ 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/
     
    Rob Williscroft, Aug 7, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. http://www.visual-basic-data-mining.net/forum

    Displaying image after selecting an image file.

    http://www.visual-basic-data-mining.net/forum, Sep 15, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    357
    http://www.visual-basic-data-mining.net/forum
    Sep 15, 2004
  2. Dustan
    Replies:
    0
    Views:
    269
    Dustan
    Jun 1, 2006
  3. Dustan
    Replies:
    6
    Views:
    352
    Dustan
    Jun 7, 2006
  4. Jeff
    Replies:
    6
    Views:
    391
  5. Jake Barnes
    Replies:
    7
    Views:
    210
    Evertjan.
    Dec 8, 2005
Loading...

Share This Page