gpt4 book ai didi

python - 如何修复 "image "pyimage1 0"doesn' t 存在”错误,为什么会发生?

转载 作者:行者123 更新时间:2023-12-01 11:24:38 26 4
gpt4 key购买 nike

我正在制作一个 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 Feather 图标。如果有人感兴趣,这是我的图标设置代码(尽管我很确定它不会导致我的错误):
tk.Tk.iconbitmap(self, default='main.ico')

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

最佳答案

像@joost-broekhuizen 一样,我在使用 Tkinter 和 matplotlib.pyplot 函数时遇到了同样的问题。向 PhotoImage 函数添加一个“主”为我解决了这个问题。

损坏的代码(引发: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()

向 PhotoImage 添加 'master=root' 解决了这个错误!
photo = ImageTk.PhotoImage(image, master=root)

关于python - 如何修复 "image "pyimage1 0"doesn' t 存在”错误,为什么会发生?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38602594/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com