8

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.

user3727843
  • 574
  • 1
  • 4
  • 13
  • Show full error message - there is more information. Is "pyimage36" a filename or what ? – furas Jun 18 '14 at 01:42
  • 2
    I have been having this problem too. I seem to only have this issue when I am using Spyder. I get the error when using Spyder both in windows and in Linux (Raspbian). When I use Idle on the Pi or Pycharm on windows I don't get this behavior. – BRM Nov 28 '17 at 19:55

5 Answers5

17

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.

user3727843
  • 574
  • 1
  • 4
  • 13
  • Some might disagree. For example. In networking it is common practice to restart a server after some amount of time or if it encounters an error (rather than trying to fix the error). This is because starting from a known state is much more predictable. – user3727843 May 29 '16 at 07:47
  • Thanks for that! That was the problem when using appJar. – shahar_m Jun 07 '17 at 07:50
3

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.

2

(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/

G4BE
  • 21
  • 2
1
from tkinter import *
from PIL import Image, ImageTk

root = Tk()
root.title("Title")

img = Image.open('Paste the directory path')
bg = ImageTk.PhotoImage(img)

lbl = Label(root, image=bg)
lbl.place(x=0, y=0)

mainloop() 

I was getting the same error. Try this code this will help you. Additionally, in case if you create a button and use it to open other window, then there use window = Toplevel(), otherwise it will again show the same error.

SmitShah_19
  • 117
  • 1
  • 1
  • 8
  • On Spyder 5.1.5 you would have to restart the console each time there is an error in your code(as you test) – rearThing May 19 '22 at 20:05
-1

From programmersought

image “pyimage1” doesn’t exist Because there can only be one root window in a program, that is, only one Tk() can exist, other windows can only exist in the form of a top-level window (Toplevel()).

Original code

import tkinter as tk
window = tk.TK()

Revised code

import tkinter as tk
window = tk.Toplevel()

Keep other code unchanged

https://www.programmersought.com/article/87961175215/