Tcl: "image does not exist" problem

Matthew Dixon Cowles matt at mondoinfo.com
Sun Sep 16 15:17:34 EDT 2001


On Sun, 16 Sep 2001 20:59:29 +0200, Janos Blazi <jblazi at hotmail.com>
wrote:

>I have the following (naive) line in a Tkinter program:
>
>    b=Button(bBar,image='d:\\test.gif')
>
>and get the error message in the subject line.

>After that I took a look in the book by Grayson and saw that things
>are more complicated. Can anybody show me what to do?

Dear Janos,
The image parameter wants an image object. For a GIF image, can use
Tkinter's PhotoImage class. I'll append a simple example.

If you haven't had a look at it already, Fredrik Lundh's excellent An
Introduction to Tkinter is also a great help when doing Tkinter
programming. It's at:

http://www.pythonware.com/library/tkinter/introduction/index.htm

I generally keep a local copy open when I'm doing Tkinter programming.
Fredrik also kindly makes available the Python Imaging Library which
(among other useful things) has a PhotoImage class that understands
various image formats that Tkinter's doesn't. It's at:

http://www.pythonware.com/downloads/index.htm#pil

Regards,
Matt



from Tkinter import *

class mainWin:

  def __init__(self,root):
    self.root=root
    self.createWidgets()
    return None

  def createWidgets(self):
    # We must keep extra reference to the image due 
    # to a limitation in Tkinter so we'll make it
    # an attribute of our object.
    self.img=PhotoImage(file="test.gif")
    b=Button(self.root,image=self.img,command=self.clickedCB)
    b.pack()
    return None

  def clickedCB(self):
    print "clicked"
    return None

def main():
  root=Tk()
  mainWin(root)
  root.mainloop()
  return None

if __name__=='__main__':
  main()



More information about the Python-list mailing list