1
votes

The following program works the for the first .jpg in the directory. When called the second time it gets a "_tkinter.TclError: image "pyimage2" doesn't exist" exception. Why does it get the error? Is there a way to reuse the first image rather than creating a second?

import sys, os if sys.version_info[0] == 2:
import Tkinter tkinter = Tkinter else: import tkinter from PIL import Image, ImageTk

def showPIL(pilImage):
    root = tkinter.Tk()
    w, h = root.winfo_screenwidth(), root.winfo_screenheight()
    root.overrideredirect(1)
    root.geometry("%dx%d+0+0" % (w, h))
    root.focus_set()
    root.bind("<Escape>", lambda e: (e.widget.withdraw(), e.widget.quit()))
    canvas = tkinter.Canvas(root,width=w,height=h)
    canvas.pack()
    canvas.configure(background='black')
    imgWidth, imgHeight = pilImage.size
 # resize photo to full screen 
    ratio = min(w/imgWidth, h/imgHeight)
    imgWidth = int(imgWidth*ratio)
    imgHeight = int(imgHeight*ratio)
    pilImage = pilImage.resize((imgWidth,imgHeight), Image.ANTIALIAS)   
    image = ImageTk.PhotoImage(pilImage)
    print(image)
    imagesprite = canvas.create_image(w/2,h/2,image=image)
    root.mainloop()

names = os.listdir("E://Users//scott//Pictures")
print(names)
for file in names:
    print(file)
    if file[-4:] == ".jpg":
        file=Image.open("E://Users//scott//Pictures//"+file)
        showPIL(file)

Here is the console output. Traceback (most recent call last): File "e:\Users\scott\Documents\Python\image test.py", line 36, in showPIL(file) File "e:\Users\scott\Documents\Python\image test.py", line 27, in showPIL imagesprite = canvas.create_image(w/2,h/2,image=image) File "C:\Program Files\Python37\lib\tkinter__init__.py", line 2486, in create_image return self._create('image', args, kw) File "C:\Program Files\Python37\lib\tkinter__init__.py", line 2477, in _create *(args + self._options(cnf, kw)))) _tkinter.TclError: image "pyimage2" doesn't exist

>

1

1 Answers

1
votes

after searching around I discovered that the first problem that tkinter.Tk() was being called multiple times whereas it must be called only once so I moved it out of the showPIL function and into the initialization. The next problem is that mainloop blocks so I replaced it with the combination of root.update_idletasks() and root.update(). The following works as I expect and need:

import sys, os
if sys.version_info[0] == 2:  # the tkinter library changed it's name from Python 2 to 3.
    import Tkinter
    tkinter = Tkinter #I decided to use a library reference to avoid potential naming conflicts with people's programs.
else:
    import tkinter
from PIL import Image, ImageTk
import time

root = tkinter.Tk()
w, h = root.winfo_screenwidth(), root.winfo_screenheight()
root.overrideredirect(1)
root.geometry("%dx%d+0+0" % (w, h))
root.focus_set()
canvas = tkinter.Canvas(root,width=w,height=h)
canvas.pack()
canvas.configure(background='black')


def showPIL(pilImage):
    imgWidth, imgHeight = pilImage.size
 # resize photo to full screen 
    ratio = min(w/imgWidth, h/imgHeight)
    imgWidth = int(imgWidth*ratio)
    imgHeight = int(imgHeight*ratio)
    pilImage = pilImage.resize((imgWidth,imgHeight), Image.ANTIALIAS)   
    image = ImageTk.PhotoImage(pilImage)
    imagesprite = canvas.create_image(w/2,h/2,image=image)
    root.update_idletasks()
    root.update()
#    root.bind("<Escape>", lambda e: (e.widget.withdraw(), e.widget.quit()))


names = os.listdir("E://Users//scott//Pictures")
print(names)
for file in names:

    print(file)
    if file[-4:] == ".jpg":
        file=Image.open("E://Users//scott//Pictures//"+file)
        showPIL(file)

        time.sleep(5)