我正在尝试将 jpg 图像放到 tkinter 画布上.tkinter 给了我这个错误:
I'm trying to put a jpg image to a tkinter canvas. tkinter gives me this error:
无法识别图像文件中的数据
couldn't recognize data in image file
我使用文档中的代码:
canv = Canvas(root, width=80, height=80, bg='white') canv.grid(row=2, column=3) img = PhotoImage(file="bll.jpg") canv.create_image(20,20, anchor=NW, image=img)
与 png 图像相同.甚至尝试将图像放入标签小部件中,但得到了同样的错误.怎么了?
Same thing with png images. Even tried to put an image into a label widget, but got the same error. What's wrong?
我在 Mac 上使用 Python 3.Python文件和图片在同一个文件夹中.
I am using Python 3 on Mac. Python file and image are in the same folder.
你的代码看起来是正确的,这对我来说是在 Windows 7 (Python 3.6) 上运行的:
Your code seems right, this is running for me on Windows 7 (Python 3.6):
from tkinter import * root = Tk() canv = Canvas(root, width=80, height=80, bg='white') canv.grid(row=2, column=3) img = PhotoImage(file="bll.jpg") canv.create_image(20,20, anchor=NW, image=img) mainloop()
导致此 tkinter GUI:
resulting in this tkinter GUI:
将此图像设为 bll.jpg
:
(imgur 将其转换为 bll.png
但这也适用于我.)
(imgur converted it to bll.png
but this is working for me as well.)
更多选项:
gif
images. Try using a .gif
image.PIL
as stated in this answer.更新:PIL
的解决方案:
from tkinter import * from PIL import ImageTk, Image root = Tk() canv = Canvas(root, width=80, height=80, bg='white') canv.grid(row=2, column=3) img = ImageTk.PhotoImage(Image.open("bll.jpg")) # PIL solution canv.create_image(20, 20, anchor=NW, image=img) mainloop()
这篇关于Tkinter 错误:无法识别图像文件中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程技术网(www.editcode.net)!