本文介绍了如何修复“图像”pyimage10"不存在“错误,为什么会发生?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个tkiner应用程序,它向用户显示一个包含一些基本信息和图片的页面,然后允许他们点击按钮查看实时比特币价格数据。但是,当我将图像添加到启动页面时,我从IDE中收到此错误:

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)

我也注意到了图标我设置的不会显示在GU中我在程序运行时窗口,只有默认的Tkinter羽化图标。这是我的图标设置代码,如果有人有兴趣(虽然我很确定它不会导致我的错误):

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')

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

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!

推荐答案

您无法使用tkinter加载 .png 格式。您需要使用库:

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)

编辑:

请创建一个 test.py 文件并运行此EXACT代码:

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.

这篇关于如何修复“图像”pyimage10"不存在“错误,为什么会发生?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-04 23:42