Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我该如何解决这个问题;“图像”;pyimage10“;不';“不存在”;错误,为什么会发生?_Python_Image Processing_Tkinter - Fatal编程技术网

Python 我该如何解决这个问题;“图像”;pyimage10“;不';“不存在”;错误,为什么会发生?

Python 我该如何解决这个问题;“图像”;pyimage10“;不';“不存在”;错误,为什么会发生?,python,image-processing,tkinter,Python,Image Processing,Tkinter,我正在制作一个tkiner应用程序,它向用户显示一个包含一些基本信息和图片的页面,然后允许用户单击按钮查看实时比特币价格数据。但是,当我将图像添加到“启动”页面时,我从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'

我正在制作一个tkiner应用程序,它向用户显示一个包含一些基本信息和图片的页面,然后允许用户单击按钮查看实时比特币价格数据。但是,当我将图像添加到“启动”页面时,我从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
我相信这些是导致我出错的代码行(它们与将图像添加到“启动”页面的代码行相同):

我还注意到,当程序运行时,我设置的图标不会显示在GUI窗口中,只有默认的Tkinter feather图标。如果有人感兴趣,下面是我的图标设置代码(尽管我很确定这不会导致我的错误):


是的,对于任何想知道的人来说,我确实将tkinter作为tk导入,所以这不是我的错误。如果有人也能告诉我为什么会发生这个错误,我会非常感兴趣:我还没有看到很多其他发生这种情况的例子,我看到的那些例子都没有提到我的图标问题。希望有人能弄明白

不能使用tkinter加载.png格式。因此,您需要使用库:

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)
编辑:

请创建一个
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()

告诉我是否有错误。

我也有同样的问题。问题是在同一程序或从中导入定义的另一个py文件中导入matplotlib.pyplot。使用画布进行绘图,就像@joost broekhuizen一样,我在使用Tkinter和matplotlib.pyplot函数时遇到了同样的问题。在PhotoImage函数中添加一个“master”为我解决了这个问题

错误代码(引发:Tcl错误:图像“pyimage10”不存在):

将“master=root”添加到PhotoImage解决了此错误

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

这个问题可以通过在
Photoimage
构造函数中添加
master=root
来解决

比如说

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

你知道为什么我选择的图标没有出现吗?我仍然会遇到同样的错误,尽管我像你建议的那样使用了PIL的Image和ImageTk模块。我仍然会遇到同样的错误,你确定吗?您是否给出了通往
比特币.png
的正确路径?请再次阅读错误消息,因为即使它是相同的,它也必须涉及另一行代码,而不是与
BTC\u img\u label
相关的代码。我收到了这个错误消息:BTC\u img\label=tk.label(self,image=BTC\u img)文件“C:\Python34\lib\tkinter\u init.py”,第2609行,在init小部件中。\uu init\uuu(self,master,'label',,cnf,kw)文件“C:\Python34\lib\tkinter_init_uu.py”,第2127行,在init(widgetName,self._w)+extra+self._options(cnf))\u tkinter.TclError:image“pyimage10”不存在,我当时没有收到错误,它正确地输出了图像这为我解决了它!在我的一些文件中,我使用了matplotlib,因为我禁用了它,所以不会再抛出错误。
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()
photo = ImageTk.PhotoImage(image, master=root)
pic=Photoimage(master=self.root,file='mypic.png')
Label(self.root,image=pic).pack()