Tcl: "image does not exist" problem

Matthew Dixon Cowles matt at
Sun Sep 16 23:04:49 CEST 2001

On Sun, 16 Sep 2001 21:42:27 +0200, Janos Blazi <jblazi at>

>Maybe I am too slow on the uptake but I still do not understand. I
>have no class like mainWin. Can't I simply say:

>    img=PhotoImage(file='d:\\test.gif')
>    b=Button(bBar,image=img,command=clicked)
>    b.pack()

>It seems I cannot. A grey button appears but it cannot be pressed and
>the image is not displayed. Is it necessary to change my program and
>introduce a class like mainWin?

Normally, you don't need a class like mainWin and you can even get
away without creating one in this case if you really want to. But my
experience in doing a bunch of GUI programming is that using one makes
for clearer code. In the case of images on buttons in Tkinter, you'll
get the results you observed unless you keep an extra reference to the
image object around. I don't know if that's considered a bug or a
limitation but in either case we have to do it.

Anything that kept a reference to the image around as long as the
button was around would work. For example, this code works:

from Tkinter import *

def clickedCB():
  print "clicked"
  return None

def main():
  return None

if __name__=='__main__':

It works because the variable img is around as long as the button is.
In a more complex program you could use, e.g., a global variable. But
I'd still use a class like mainWin because I think it's easier to see
what's going on from the code.

>Anyway, thx for you quick asnwer.

You're welcome!


More information about the Python-list mailing list