[Tutor] Again problem with PhotoImage

Gregor Lingl glingl at aon.at
Fri Sep 12 20:17:49 EDT 2003


Hi, I have a problem using PhotoImage, which  I don't
understand (and don't know how to solve or get around):

I'm using Python 2.2 and IDLE, and I intend the program
I develop to be used in this environment.

I'll explain the problem with a tiny demo-script:

from Tkinter import *

root = Tk()
cv = Canvas(root)
cv.pack()
pi = PhotoImage(file="huhn01.gif")
cv.create_image(100,100,image=pi)

When I run this (within IDLE 0.8, so no mainloop is needed)
a canvas appears with a huhn-picture as expected.

Now I don't close this window and run the program again.

A second window opens, but, alas, the huhn-pic disappears from the
first window, but doesn't appear in the new one. Instead I get
the following error-message:

Traceback (most recent call last):
Traceback (most recent call last):
  File "C:/_/tdev/giftest02.py", line 7, in ?
    cv.create_image(100,100,image=pi)
  File "C:\Python22\lib\lib-tk\Tkinter.py", line 1973, in create_image
    return self._create('image', args, kw)
  File "C:\Python22\lib\lib-tk\Tkinter.py", line 1963, in _create
    (self._w, 'create', itemType)
TclError: image "pyimage2" doesn't exist

-----but----there IS an image-:
 >>> pi
<Tkinter.PhotoImage instance at 0x00AA47A0>
 >>>
I suppose, that this has to do with the fact that
the program and IDLE is running in the same process and
there is some clutter with the namespaces (for in
Python 2.3 with IDLE 1.0 the first program will be killed
before the second one is started - and there I have to use mainloop,
which prevents me to interact with the canvas as long as then
mainloop is running ...) ... and with some strange property of
PhotoImage mentioned elsewhere, but I forgot where ... (Sorry)

As I'd like to use the canvas interactively, for instance to move the
image,  I think I have to stick with IDLE 0.8 or use the -n-option
of IDLE 1.0, which would make no difference, I think ...

Is there a remedy?

Gregor




 




More information about the Tutor mailing list