Tkinter pyimage doesn't exist
Asked Answered
S

7

9

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.

Skantze answered 17/6, 2014 at 22:6 Comment(2)
Show full error message - there is more information. Is "pyimage36" a filename or what ?Brail
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.Rockbottom
S
18

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.

Skantze answered 18/6, 2014 at 21:50 Comment(2)
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.Skantze
Thanks for that! That was the problem when using appJar.Alkanet
A
4

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.

Anticipate answered 1/9, 2019 at 1:56 Comment(0)
S
4

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/

Saprophyte answered 22/6, 2021 at 9:35 Comment(1)
This solved the problem for my student’s code. Had to use a ‘TopLevel’ object for secondary windows.Towrey
I
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/

Irvingirwin answered 10/10, 2021 at 7:15 Comment(0)
S
2
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.

Stackhouse answered 21/12, 2021 at 21:5 Comment(1)
On Spyder 5.1.5 you would have to restart the console each time there is an error in your code(as you test)Psychotechnics
P
0

In spyder, change the setting of spyder fix this error. change the spyder perferences--> ipython console--> Graphics--> Graphics backend --> Automatic

Phallic answered 20/4 at 19:12 Comment(0)
J
0

I have a solution and explanation.

I came across this same issue. I was working with someone and the above answer about the tk.Tk() and tk.Toplevel().

You are only supposed to have one .TK() and then you need to use Toplevel()s.

The PIL package shows the pyImage1.. error because it is trying to load from the .TK() and it won't run on the second .TK() because it doesn't pass it properly. Because it's not built that way.

In my case, I had my first file that had the tk.Tk() and opened up a second file that had another tk.Tk() in it, and got the pyImage1(any next number) error. I thought it was the calling from the main file to the second. This article

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

does explain that there shouldn't be more than one tk.Tk().

When using a single tk.Tk() and then others as tk.Toplevel(), it works properly.

Just for good measure if you are using the tk.Tk() and calling to a second file this works well:

if __name__ == "__main__": app = tk.Tk()
else: app = tk.Toplevel()

And if it's called as main you're good, and toplevel if secondarily.

Credit to auwal amshi above for putting me on the path.

Jarry answered 4/5 at 2:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.