【发布时间】:2010-07-05 04:32:48
【问题描述】:
所以我在 macbook pro 上运行 python 2.6 并尝试在 python 中编写代码以在 tkinter gui 的标签中显示来自文件的图像。该图像称为 image.png。当我使用此代码时,程序运行没有错误
i = Image.open("image.png")
但是当我执行此代码时(我添加了一行):
i = Image.open("image.png")
photo = ImageTk.PhotoImage(i)
程序将崩溃并在命令行中显示“总线错误”。我什至不知道那是什么意思。我认为 PIL 安装正确,因为 Image 可以工作,但 ImageTk 不工作的事实让我感到困惑。谁能告诉我可能导致此总线错误的原因是什么?
编辑: 好吧,我制作了一个新程序来进一步测试错误。这是我运行的确切脚本:
import Image
import ImageTk
i = Image.open("image.png")
photo = ImageTk.PhotoImage(i)
现在不是得到“总线错误”,而是我的回溯。
Traceback (most recent call last):
File "imageTest.py", line 5, in <module>
photo = ImageTk.PhotoImage(i)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/PIL/ImageTk.py", line 113, in __init__
self.__photo = apply(Tkinter.PhotoImage, (), kw)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk/Tkinter.py", line 3285, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk/Tkinter.py", line 3226, in __init__
raise RuntimeError, 'Too early to create image'
RuntimeError: Too early to create image
Exception AttributeError: "PhotoImage instance has no attribute '_PhotoImage__photo'" in <bound method PhotoImage.__del__ of <ImageTk.PhotoImage instance at 0x3c7a30>> ignored
【问题讨论】:
-
请编辑您的问题以显示您收到的完整回溯和确切的错误消息。
标签: python image python-imaging-library bus