简体   繁体   English

如何修复“图像”pyimage10“不存在”错误,为什么会发生?

[英]How do I fix the “image ”pyimage10“ doesn't exist” error, and why does it happen?

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.我正在制作一个 tkiner 应用程序,在允许用户单击按钮查看实时比特币价格数据之前,它会向用户显示一个包含一些基本信息和图片的页面。 However, when I added the image to the 'start up' page, I got this error from my IDE:但是,当我将图像添加到“启动”页面时,我的 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.我还注意到我设置的图标在程序运行时没有显示在 GUI 窗口中,只有默认的 Tkinter 羽毛图标。 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.是的,对于任何想知道的人,我确实将 tkinter 作为 tk 导入,所以这不是我的错误。 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!希望有人能解决这个问题!

Like @joost-broekhuizen, I've had the same problem using Tkinter together with matplotlib.pyplot functions.像@joost-broekhuizen 一样,我在使用 Tkinter 和 matplotlib.pyplot 函数时遇到了同样的问题。 Adding a 'master' to the PhotoImage function solved the problem for me.向 PhotoImage 函数添加一个“主”为我解决了这个问题。

Broken code (raises: TclError: image "pyimage10" doesn't exist):损坏的代码(引发:TclError:图像“pyimage10”不存在):

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!向 PhotoImage 添加 'master=root' 解决了这个错误!

photo = ImageTk.PhotoImage(image, master=root)

You can not load a .png format with tkinter.您无法使用 tkinter 加载.png格式。 You rather need to use the PIL library for that:您更需要为此使用PIL库:

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:请创建一个test.py文件并运行此确切代码:

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.告诉我你是否有错误。

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.问题是在同一个程序或另一个从中导入定义的 py 文件中导入 matplotlib.pyplot。 Use Canvas for your plots instead使用 Canvas 代替您的绘图

This problem can be solved by adding master=root in Photoimage Constructor这个问题可以通过在Photoimage Constructor中添加master=root来解决

like for eg喜欢例如

pic=Photoimage(master=self.root,file='mypic.png')
Label(self.root,image=pic).pack()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM