tkinter 8.6 仍然不支持 png 文件?
tkinter 8.6 still doesn't support png files?
我在 Windows 上使用 python 3.7.9
。 tkinter
版本为 8.6。
我正在关注 Here,并尝试使用 .png
图像作为 GUI 背景。
当我 运行 #1 时,它会抛出一个错误。当我运行#2时,它工作正常。
- PhotoImage for images in PGM, PPM, GIF and PNG formats. The latter is supported starting with Tk 8.6.
几年前我发现了类似的问题,但那时候 tkinter
还不支持 .png
,他们建议使用 PIL
。
我检查了我的 tkinter
版本,它说是 8.6,谁能告诉我这是什么问题?
PIL
的使用是否仍被正式要求处理 png
?
附带说明,当我将 .png
替换为 .gif
时,#1 也引发了完全相同的错误。
错误
---------------------------------------------------------------------------
TclError Traceback (most recent call last)
<ipython-input-2-4a440c40f4bf> in <module>
4 root = Tk()
5
----> 6 bg = PhotoImage(file='test.png')
7 bg_label = Label(root, image=bg)
8 bg_label.place(x=0, y=0, relwidth=1, relheight=1)
~\anaconda3\envs\my_env\lib\tkinter\__init__.py in __init__(self, name, cnf, master, **kw)
3543 Valid resource names: data, format, file, gamma, height, palette,
3544 width."""
-> 3545 Image.__init__(self, 'photo', name, cnf, master, **kw)
3546 def blank(self):
3547 """Display a transparent image."""
~\anaconda3\envs\my_env\lib\tkinter\__init__.py in __init__(self, imgtype, name, cnf, master, **kw)
3499 v = self._register(v)
3500 options = options + ('-'+k, v)
-> 3501 self.tk.call(('image', 'create', imgtype, name,) + options)
3502 self.name = name
3503 def __str__(self): return self.name
TclError: couldn't recognize data in image file "test.png"
#1
from tkinter import *
root = Tk()
bg = PhotoImage(file='test.png')
bg_label = Label(root, image=bg)
bg_label.place(x=0, y=0, relwidth=1, relheight=1)
root.mainloop()
#2
from tkinter import *
from PIL import Image, ImageTk
root = Tk()
bg = ImageTk.PhotoImage(file='test.gif')
bg_label = Label(root, image=bg)
bg_label.place(x=0, y=0, relwidth=1, relheight=1)
root.mainloop()
tkinter
8.6 版本支持 .png
图像。
它不起作用的原因是我认为是 .png
文件的图像实际上不是 .png
文件。我最初将图像下载为 .jpg
文件,只是更改了扩展名。 更改扩展名不会更改文件类型本身。
我抓取了其他 .png
图片,效果很好。
我在 Windows 上使用 python 3.7.9
。 tkinter
版本为 8.6。
我正在关注 Here,并尝试使用 .png
图像作为 GUI 背景。
当我 运行 #1 时,它会抛出一个错误。当我运行#2时,它工作正常。
- PhotoImage for images in PGM, PPM, GIF and PNG formats. The latter is supported starting with Tk 8.6.
几年前我发现了类似的问题,但那时候 tkinter
还不支持 .png
,他们建议使用 PIL
。
我检查了我的 tkinter
版本,它说是 8.6,谁能告诉我这是什么问题?
PIL
的使用是否仍被正式要求处理 png
?
附带说明,当我将 .png
替换为 .gif
时,#1 也引发了完全相同的错误。
错误
---------------------------------------------------------------------------
TclError Traceback (most recent call last)
<ipython-input-2-4a440c40f4bf> in <module>
4 root = Tk()
5
----> 6 bg = PhotoImage(file='test.png')
7 bg_label = Label(root, image=bg)
8 bg_label.place(x=0, y=0, relwidth=1, relheight=1)
~\anaconda3\envs\my_env\lib\tkinter\__init__.py in __init__(self, name, cnf, master, **kw)
3543 Valid resource names: data, format, file, gamma, height, palette,
3544 width."""
-> 3545 Image.__init__(self, 'photo', name, cnf, master, **kw)
3546 def blank(self):
3547 """Display a transparent image."""
~\anaconda3\envs\my_env\lib\tkinter\__init__.py in __init__(self, imgtype, name, cnf, master, **kw)
3499 v = self._register(v)
3500 options = options + ('-'+k, v)
-> 3501 self.tk.call(('image', 'create', imgtype, name,) + options)
3502 self.name = name
3503 def __str__(self): return self.name
TclError: couldn't recognize data in image file "test.png"
#1
from tkinter import *
root = Tk()
bg = PhotoImage(file='test.png')
bg_label = Label(root, image=bg)
bg_label.place(x=0, y=0, relwidth=1, relheight=1)
root.mainloop()
#2
from tkinter import *
from PIL import Image, ImageTk
root = Tk()
bg = ImageTk.PhotoImage(file='test.gif')
bg_label = Label(root, image=bg)
bg_label.place(x=0, y=0, relwidth=1, relheight=1)
root.mainloop()
tkinter
8.6 版本支持 .png
图像。
它不起作用的原因是我认为是 .png
文件的图像实际上不是 .png
文件。我最初将图像下载为 .jpg
文件,只是更改了扩展名。 更改扩展名不会更改文件类型本身。
我抓取了其他 .png
图片,效果很好。