user3727843
user3727843

Reputation: 594

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.

Upvotes: 9

Views: 39168

Answers (7)

Liamdaniel
Liamdaniel

Reputation: 21

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.

Upvotes: 0

Tylerli
Tylerli

Reputation: 1

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

Upvotes: 0

SmitShah_19
SmitShah_19

Reputation: 157

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.

Upvotes: 2

G4BE
G4BE

Reputation: 21

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

Upvotes: 2

auwal amshi
auwal amshi

Reputation: 61

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/

Upvotes: 4

user12004660
user12004660

Reputation: 41

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.

Upvotes: 4

user3727843
user3727843

Reputation: 594

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.

Upvotes: 18

Related Questions