Python tkinter 8.6仍然没有';你不支持png文件吗?
我在Windows上使用python 3.7.9<代码>tkinter版本为8.6 我正在跟踪,并尝试使用Python tkinter 8.6仍然没有';你不支持png文件吗?,python,tkinter,python-imaging-library,Python,Tkinter,Python Imaging Library,我在Windows上使用python 3.7.9tkinter版本为8.6 我正在跟踪,并尝试使用.png图像作为GUI背景。 当我运行#1时,它会抛出一个错误。当我运行#2时,效果很好 说: PhotoImage用于PGM、PPM、GIF和PNG格式的图像。后者从Tk 8.6开始得到支持 我在几年前发现了类似的问题,但那时tkinter还不支持.png,他们建议使用PIL 我检查了我的tkinter版本,上面写着8.6,有人能告诉我这里有什么问题吗? 处理png是否仍然需要使用PIL 另一
.png
图像作为GUI背景。当我运行#1时,它会抛出一个错误。当我运行#2时,效果很好 说:
- PhotoImage用于PGM、PPM、GIF和PNG格式的图像。后者从Tk 8.6开始得到支持
tkinter
还不支持.png
,他们建议使用PIL
我检查了我的tkinter
版本,上面写着8.6,有人能告诉我这里有什么问题吗?
处理png
是否仍然需要使用PIL
另一方面,当我将.png
替换为.gif
时,对#1产生了完全相同的错误
错误 #2
tkinter
8.6版确实支持.png
iamges
它不起作用的原因是我认为是.png
文件的图像实际上不是.png
文件。我最初下载的图像是.jpg
文件,我只是更改了扩展名更改扩展名不会更改文件类型本身。
我抓取了另一张
.png
图像,效果很好。你确定该图像是png文件吗?可能是其他格式被重命名为.png吗?@DapperDuck不同之处在于.png和.gif以及从PIL导入图像,ImageTk@Bryan,谢谢你的评论。仔细想想,原来是。jpg
。我把它转换成png。更改扩展名不会更改格式本身…?当然,更改文件扩展名不会更改格式。使用图像处理应用程序对其进行转换(如从ImageMagick
转换)。或者使用枕头模块,它将自动检测图像格式。@koyamashinji不,它不会。扩展名只是文件名的一部分,是对人类的注释。大多数程序都忽略了扩展名。
---------------------------------------------------------------------------
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"
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()
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()