How do I fix the "image "pyimage10" doesn't exist" error, and why does it happen?
Asked Answered
A

5

6

I am making a tkiner application and that shows a user a page with some basic information and a picture before allowing them to click a button to view live Bitcoin price data. However, when I added the image to the 'start up' page, I got this error from my IDE:

 BTC_img_label = tk.Label(self, image=BTC_img)
 File "C:\Python34\lib\tkinter\__init__.py", line 2609, in __init__
 Widget.__init__(self, master, 'label', cnf, kw)
 File "C:\Python34\lib\tkinter\__init__.py", line 2127, in __init__
 (widgetName, self._w) + extra + self._options(cnf))
 _tkinter.TclError: image "pyimage10" doesn't exist

I believe that these are the lines of code that are causing my error (they are the same lines that add the image to the 'start up' page):

BTC_img = tk.PhotoImage(file='bitcoin.png')
BTC_img_label = tk.Label(self, image=BTC_img)
BTC_img_label.image = BTC_img
BTC_img_label.grid(row=2, column=0)

I also noticed that the icon that I set does not show in the GUI window when the program is run, only the default Tkinter feather icon. Here's my icon setting code if anyone is interested (though I'm pretty sure it's not causing my error):

tk.Tk.iconbitmap(self, default='main.ico')

And yes, for anyone wondering, I did import tkinter as tk, so that is not my error. If anyone could also tell me why this error happens, I would be very interested: I haven't seen a lot of other examples of this happening, and the ones I have seen had no mention to my icon problem. Hope somebody can figure this out!

Adria answered 27/7, 2016 at 2:11 Comment(0)
B
2

You can not load a .png format with tkinter. You rather need to use the PIL library for that:

import PIL

image = PIL.Image.open("bitcoin.png")
BTC_img = PIL.ImageTk.PhotoImage(image)
BTC_img_label = tk.Label(self, image=BTC_img)
BTC_img_label.image = BTC_img
BTC_img_label.grid(row=2, column=0)

EDIT:

Please, create a test.py file and run this EXACT code:

import tkinter as tk
from PIL import Image, ImageTk

root = tk.Tk()    
image = Image.open("bitcoin.png")
photo = ImageTk.PhotoImage(image)
label = tk.Label(root, image=photo)
label.image = photo
label.grid(row=2, column=0)
#Start the program
root.mainloop()

Tell me if you get an error or not.

Belemnite answered 27/7, 2016 at 4:10 Comment(14)
Do you have any idea why my chosen icon didn't show up?Adria
I'm still getting the same error, even though I used PIL's Image and ImageTk modules like you suggested.Adria
I'm still getting the same error, Are you sure? Did you give the right path leading to bitcoin.png? Please read again the error message, because even if it is the same, it must concern an other line of code, not the one related to BTC_img_label.Belemnite
I got this error message:BTC_img_label = tk.Label(self, image=BTC_img) File "C:\Python34\lib\tkinter_init_.py", line 2609, in init Widget.__init__(self, master, 'label', cnf, kw) File "C:\Python34\lib\tkinter_init_.py", line 2127, in init (widgetName, self._w) + extra + self._options(cnf)) _tkinter.TclError: image "pyimage10" doesn't existAdria
Ok. Did you give the right path leading to bitcoin.png?Belemnite
I have bitcoin.png in the same directory as my code, so I did give the right path. Is it possible that other modules could be interfering with my code and causing this error? I was working with matplotlib, which I just recently started working with and am not too familiar with yetAdria
No, it has nothing to do with Matplotlib or any other librariesBelemnite
Do you have any thoughts on why my icon would be affected? It seems like that could be a symptom of the problem.Adria
What I am 100% sure of is that there is a mistake the way you loaded the image and the correct way is the one I showed here. I even took a .png file and run the lines of code above myself on my machine and all works fine. So it is a mystery for me as why you keep getting the same error messageBelemnite
For your icon issue, try this solutionBelemnite
I probably have another conflicting error somewhere else in my program, though I find it weird that my IDE still says that the error originates from the same line. Is there any way that I can show you the rest of my program? I would post it but the script is pretty long, and i'd have to post it all since I don't know what's causing the error (I understand that kind of thing is frowned upon here).Adria
I didn't get an error that time, and it outputted the image correctlyAdria
Ok. Good (I wanted to see if the image itself was not the problem -corrupted file image, for instance). I wonder if it is possible for you to share your full code instead?Belemnite
I have the code split into multiple programs, and the total line number when all lines are combined together is 484, so short of emailing you the files I don't think I can post it to my question.Adria
C
5

Like @joost-broekhuizen, I've had the same problem using Tkinter together with matplotlib.pyplot functions. Adding a 'master' to the PhotoImage function solved the problem for me.

Broken code (raises: TclError: image "pyimage10" doesn't exist):

import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import matplotlib.pyplot as plt
import Tkinter as tk
from PIL import Image, ImageTk

fig = plt.figure()

root = tk.Tk()
image = Image.open("background.png")
photo = ImageTk.PhotoImage(image)
label = tk.Label(root, image=photo)
label.image = image
label.pack()

root.mainloop()

Adding 'master=root' to PhotoImage solved this error!

photo = ImageTk.PhotoImage(image, master=root)
Cockeye answered 30/7, 2018 at 11:8 Comment(0)
B
2

You can not load a .png format with tkinter. You rather need to use the PIL library for that:

import PIL

image = PIL.Image.open("bitcoin.png")
BTC_img = PIL.ImageTk.PhotoImage(image)
BTC_img_label = tk.Label(self, image=BTC_img)
BTC_img_label.image = BTC_img
BTC_img_label.grid(row=2, column=0)

EDIT:

Please, create a test.py file and run this EXACT code:

import tkinter as tk
from PIL import Image, ImageTk

root = tk.Tk()    
image = Image.open("bitcoin.png")
photo = ImageTk.PhotoImage(image)
label = tk.Label(root, image=photo)
label.image = photo
label.grid(row=2, column=0)
#Start the program
root.mainloop()

Tell me if you get an error or not.

Belemnite answered 27/7, 2016 at 4:10 Comment(14)
Do you have any idea why my chosen icon didn't show up?Adria
I'm still getting the same error, even though I used PIL's Image and ImageTk modules like you suggested.Adria
I'm still getting the same error, Are you sure? Did you give the right path leading to bitcoin.png? Please read again the error message, because even if it is the same, it must concern an other line of code, not the one related to BTC_img_label.Belemnite
I got this error message:BTC_img_label = tk.Label(self, image=BTC_img) File "C:\Python34\lib\tkinter_init_.py", line 2609, in init Widget.__init__(self, master, 'label', cnf, kw) File "C:\Python34\lib\tkinter_init_.py", line 2127, in init (widgetName, self._w) + extra + self._options(cnf)) _tkinter.TclError: image "pyimage10" doesn't existAdria
Ok. Did you give the right path leading to bitcoin.png?Belemnite
I have bitcoin.png in the same directory as my code, so I did give the right path. Is it possible that other modules could be interfering with my code and causing this error? I was working with matplotlib, which I just recently started working with and am not too familiar with yetAdria
No, it has nothing to do with Matplotlib or any other librariesBelemnite
Do you have any thoughts on why my icon would be affected? It seems like that could be a symptom of the problem.Adria
What I am 100% sure of is that there is a mistake the way you loaded the image and the correct way is the one I showed here. I even took a .png file and run the lines of code above myself on my machine and all works fine. So it is a mystery for me as why you keep getting the same error messageBelemnite
For your icon issue, try this solutionBelemnite
I probably have another conflicting error somewhere else in my program, though I find it weird that my IDE still says that the error originates from the same line. Is there any way that I can show you the rest of my program? I would post it but the script is pretty long, and i'd have to post it all since I don't know what's causing the error (I understand that kind of thing is frowned upon here).Adria
I didn't get an error that time, and it outputted the image correctlyAdria
Ok. Good (I wanted to see if the image itself was not the problem -corrupted file image, for instance). I wonder if it is possible for you to share your full code instead?Belemnite
I have the code split into multiple programs, and the total line number when all lines are combined together is 484, so short of emailing you the files I don't think I can post it to my question.Adria
S
1

I had the same problem. The problem was importing matplotlib.pyplot in the same program or in another py file from which you import definitions. Use Canvas for your plots instead

Slashing answered 20/12, 2016 at 9:40 Comment(1)
This solved it for me! In some of my files I used matplotlib and since I disabled it no more errors are thrown.Refinery
W
0

This problem can be solved by adding master=root in Photoimage Constructor

like for e.g.

pic=Photoimage(master=self.root,file='mypic.png')
Label(self.root,image=pic).pack()
Wiebmer answered 19/4, 2021 at 5:17 Comment(0)
I
0

I had the same problem, but it stemmed from another reason. In my case, it was because I was adding the tk root again in a submodule (a new window) so I could run that window standalone while designing it:

class NewWindow(tk.Toplevel):
    root = tk.Tk()

Removing it solved the problem.

Indivisible answered 11/9, 2023 at 14:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.