所以我在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