Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tkinter pyimage doesn't exist

I know there are lot of similar questions, but there aren't any simple enough that I am able to understand. I have the following code:

import Tkinter as tk
from PIL import Image, ImageTk

class MainWindow:
    def __init__(self, master):
        canvas = Canvas(master)
        canvas.pack()
        self.pimage = Image.open(filename)
        self.cimage = ImageTk.PhotoImage(self.pimage)
        self.image = canvas.create_image(0,0,image=self.cimage)


filename = full_filename
root = tk.Tk()
x = MainWindow(root)
mainloop()

and I get the following error:

TclError: image "pyimage36" doesn't exist

I've read some stuff about the image objects getting garbage cleaned but I don't quite understand it.

like image 700
user3727843 Avatar asked Jun 17 '14 22:06

user3727843


3 Answers

Figured it out. For some reason, while running in the debugger, if any previous executions had thrown errors I get the "pyimage doesn't exist" error. However, if I restart the debugger (or no previously executed scripts have thrown errors), then the program runs fine.

like image 96
user3727843 Avatar answered Oct 01 '22 12:10

user3727843


I had the same error message when using spyder 3.3.6 the only way i could get the .png file to load and display after getting the 'Tinker pyimage error ' was to go to the Console and restart the kernel. After that i worked fine.

like image 26
user12004660 Avatar answered Oct 01 '22 12:10

user12004660


(Python 3.8)
If you are using a IDE with a console(such as Spyder) just call root.mainloop() in the console.

Odds are that you have a bunch of partially loaded tkinter GUI's that never managed to be executed due to an error that prevented the root.mainloop() function from being run.
Once you have run the root.mainloop() a bunch of GUI's will likely appear on screen. After you have closed all those GUI's try running your code again.
Image of multiple Tkinter GUI's appearing on screen

Read more about mainloop() here: https://pythonguides.com/python-tkinter-mainloop/

like image 37
G4BE Avatar answered Oct 01 '22 13:10

G4BE