5

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!

4 Answers4

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)
PlukAndPlay
  • 51
  • 1
  • 5
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.

Billal Begueradj
  • 20,717
  • 43
  • 112
  • 130
  • Do you have any idea why my chosen icon didn't show up? –  Jul 27 '16 at 05:27
  • 2
    I'm still getting the same error, even though I used PIL's Image and ImageTk modules like you suggested. –  Jul 27 '16 at 05:36
  • *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`. – Billal Begueradj Jul 27 '16 at 05:48
  • 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 exist –  Jul 27 '16 at 05:52
  • Ok. Did you give the **right path** leading to `bitcoin.png`? – Billal Begueradj Jul 27 '16 at 05:53
  • 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 yet –  Jul 27 '16 at 05:54
  • No, it has nothing to do with Matplotlib or any other libraries – Billal Begueradj Jul 27 '16 at 05:56
  • Do you have any thoughts on why my icon would be affected? It seems like that could be a symptom of the problem. –  Jul 27 '16 at 05:59
  • 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 message – Billal Begueradj Jul 27 '16 at 06:02
  • For your icon issue, try [**this solution**](http://stackoverflow.com/questions/11176638/python-setting-application-icon) – Billal Begueradj Jul 27 '16 at 06:05
  • 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). –  Jul 27 '16 at 06:07
  • 1
    I didn't get an error that time, and it outputted the image correctly –  Jul 27 '16 at 06:28
  • 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? – Billal Begueradj Jul 27 '16 at 06:35
  • 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. –  Jul 27 '16 at 06:39
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

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()
Abhishek Patil
  • 1,373
  • 3
  • 30
  • 62