PhotoImage 用于 PGM、PPM、GIF 和 PNG 格式的图像。从 Tk 8.6 开始支持后者。
关于我的环境的信息,
- WIN10
- Python 3.6.0
- tkinter.TkVersion 8.6
- tkinter.Tcl().eval('info patchlevel') 8.6.6
我有一些 PNG 文件在以下脚本中失败
from tkinter import *
root = Tk()
bg = PhotoImage(file='d:/Fandom.png') # Failed here
bg_label = Label(root, image=bg)
bg_label.place(x=0, y=0, relwidth=1, relheight=1)
root.mainloop()
可以从以下位置下载不同的 PNG 文件
两个文件的文件头与89 50 4E 47 0D 0A 1A 0A
PNG 文件相同。在很多软件下,这两个文件都可以毫无问题地显示,比如 windows 文件管理器、ACDSEE、Paint ......
不同PNG文件的失败信息
# Fandom.png
Traceback (most recent call last):
File "<module1>", line 5, in <module>
File "C:\Software\Python\lib\tkinter\__init__.py", line 3539, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Software\Python\lib\tkinter\__init__.py", line 3495, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: encountered an unsupported criticial chunk type "orNT"
# duck.png
Traceback (most recent call last):
File "<module1>", line 5, in <module>
File "C:\Software\Python\lib\tkinter\__init__.py", line 3539, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Software\Python\lib\tkinter\__init__.py", line 3495, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: encountered an unsupported criticial chunk type "eXIf"
图书馆Pillow (PIL)
可能有助于工作,但不是这里的问题。
我的问题是
- 带有 tkinter 8.6 的 python 3.6.x 是否支持所有 PNG 文件?
- 如果没有,什么格式或版本的 PNG 文件得到支持?