1
votes

I keep getting the error, TclError: image "pyimage8" doesn't exist. It is strange, as the number increases every time I run it?

I'm running python using spyder, dunno whether this affects anything.

Here is my code:

#import tkinter
import Tkinter as tk
  
homescreenImage = PhotoImage(file="Homescreen.gif") 

#create a GUI window.
root = Tk()
#set the title.
root.title("Welcome to the Pit!")
#set the size.
root.geometry("1100x700")

homescreenFrame = tk.Frame(root, width=1100, height = 700)
homescreenFrame.pack()

homescreenLabel = tk.Label(homescreenFrame, image=homescreenImage)
homescreenLabel.pack()


#start the GUI
root.mainloop()
5
Is the image within the same folder where this script is running? - diegoiva
This code throws all kinds of errors. Please post a minimal reproducible example. - Aran-Fey

5 Answers

2
votes

I found that my script would run once and then give me an error on subsequent runs. If I restarted the console, it would run again. I solved the problem by using the following code in the beginning of my script:

import sys
if "Tkinter" not in sys.modules:
    from Tkinter import *

It works every time now.

0
votes

If you import Tkinter as tk you should use the alias tk when calling tk, eg. root = tk.Tk(). Otherwise Python will not find Tk.

You don't need to import PIL for this.

You can not create a Photoimage before you create Tk.

Try this:

import Tkinter as tk

root = tk.Tk()
root.title("Welcome to the Pit!")
root.geometry("1100x700")

homescreenImage = tk.PhotoImage(file="Homescreen.gif") 
homescreenFrame = tk.Frame(root, width=1100, height = 700,)
homescreenFrame.pack()
homescreenLabel = tk.Label(homescreenFrame, image=homescreenImage)
homescreenLabel.pack()

root.mainloop()

Be kind and paste the whole error message in your question also.

0
votes

Following could be the errors:

1) Give the whole path to the file name eg: "/home/user/Homescreen.gif"

2) If you are using windows and the above doesn't work: use "\\C:\\home\\Homescreen.gif" (this is because, windows gets confused)

3) If that also, doesn't work, ensure that the directory of your python program is the same as that of the image.

4) Also, create the photoimage only after you have created the root window.

5) 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.

6) Also, don't import PIL, it's not required.

Try all the above, if it doesn't work let me know. Hope this helps.

0
votes

i think this could be due to :

tkinter only supports .png format for images Yet, there are other ways to add .gif`` instead of PhotoImage```

0
votes

In my case, it was because I forgot to keep a reference to the image. Try adding this line after creating the label:

homescreenLabel.image=homescreenImage.