我注意到 PNG 图像不会在 OS X Mavericks 中使用 ImageTk.PhotoImage 的 Tkinter 应用程序中显示。但是,GIF 和 JPEG 显示效果良好。没有打印错误或抛出异常,并且调试代码显示图像已读取并且具有正确的高度和宽度。这是一个简化的示例:
import Tkinter
from PIL import Image, ImageTk
logo_file = 'test.png'
#logo_file = 'test.gif'
class Application(Tkinter.Frame):
def __init__(self, master):
Tkinter.Frame.__init__(self, master)
self.master.minsize(width=512, height=256)
self.master.config()
self.pack()
self.main_frame = Tkinter.Frame()
self.some_image = ImageTk.PhotoImage(Image.open(logo_file))
some_label = Tkinter.Label(self.main_frame, image=self.some_image)
some_label.config()
some_label.pack(side='top')
self.main_frame.place(in_=self.master, anchor='c', relx=.5, rely=.5)
root = Tkinter.Tk()
app = Application(root)
app.mainloop()
如果您使用 GIF,则会显示图像,但使用 PNG 则不会显示。再说一次,这只发生在 OS X Mavericks 上,Mountain Lion 工作得很好。我尝试重新安装(编译 PIL)但没有成功,并尝试了新的 virtualenv。
创建/保存 PNG 时是否需要正确设置某些 PNG 属性?或者这是 PIL 或 Tkinter 或 OS X 中的错误?
更新以添加一些细节
我在用着:
- Python 2.7.5 (/usr/bin/python)
- PIL 1.1.7(使用pip编译)
这是在一台刚刚从 Mountain Lion 更新到 Mavericks 的机器上,之前安装了 PIL,而且我没有弄乱 Apple 附带的 Python 系统。
更新 2 枕头设置摘要
我安装了 Pillow 2.2.1,它说它支持 PNG:
--------------------------------------------------------------------
PIL SETUP SUMMARY
--------------------------------------------------------------------
version Pillow 2.2.1
platform darwin 2.7.5 (default, Aug 25 2013, 00:04:04)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)]
--------------------------------------------------------------------
--- TKINTER support available
--- JPEG support available
--- ZLIB (PNG/ZIP) support available
--- TIFF G3/G4 (experimental) support available
--- FREETYPE2 support available
*** LITTLECMS support not available
*** WEBP support not available
*** WEBPMUX support not available
--------------------------------------------------------------------
我还使用brew(libpng 1.5.14)卸载并重新安装了libpng。然后我重新安装了 Pillow 以确保它是用它构建的,尽管我认为它使用了 zlib。
Update 3 尝试构建 Python 2.7.5
也许问题出在 zlib 上,尝试编译 Python 2.7.5 我得到这个:
Python build finished, but the necessary bits to build these modules were not found:
_bsddb _sqlite3 _ssl
bsddb185 dbm dl
gdbm imageop linuxaudiodev
nis ossaudiodev readline
spwd sunaudiodev zlib
To find the necessary bits, look in setup.py in detect_modules() for the module's name.
Failed to build these modules:
_tkinter