我正在创建一个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
我认为这些是造成我的错误的代码行(它们与将图像添加到“启动”页面的行相同):
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)
我还注意到,我设置的图标在程序运行时没有显示在GUI窗口中,只有默认的Tkinter羽毛图标。如果有人感兴趣的话,这是我的图标设置代码(虽然我很确定它不会导致我的错误):
tk.Tk.iconbitmap(self, default='main.ico')
是的,对于任何想知道的人,我确实将tkinter导入为tk,所以这不是我的错误。如果有人也可以告诉我为什么会发生这种错误,我会非常感兴趣:我还没有看到很多其他的例子,我看到的那些没有提到我的图标问题。希望有人能搞清楚这一点!
答案 0 :(得分:1)
我遇到了同样的问题。问题是在同一程序或导入定义的另一个py文件中导入matplotlib.pyplot。使用Canvas代替
答案 1 :(得分:1)
就像@ joost-broekhuizen一样,我在使用Tkinter和matplotlib.pyplot函数时遇到了同样的问题。在PhotoImage函数中添加一个“母版”可以为我解决问题。
代码损坏(引发:TclError:图像“ pyimage10”不存在):
row.names
在PhotoImage中添加“ master = root”可以解决此错误!
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()
答案 2 :(得分:0)
您无法使用tkinter加载 .png 格式。您需要使用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)
修改强>
请创建一个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()
告诉我你是否收到错误。
答案 3 :(得分:0)
这个问题可以通过在master=root
构造函数中加入Photoimage
来解决
喜欢例如
pic=Photoimage(master=self.root,file='mypic.png')
Label(self.root,image=pic).pack()