我是python编程的初学者。我将把图像放在框架上。但图像未显示错误如下所示。 回溯(最近一次呼叫最后一次):
File "C:/Users/kobinath/PycharmProjects/pythonProject4/jj.py", line 5, in <module>
img = PhotoImage(file="pic.jpg")
File "C:\Users\kobinath\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 4061, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Users\kobinath\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 4006, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't recognize data in image file "pic.jpg"
到目前为止我所尝试的,我附在下面
from tkinter import *
root = Tk()
canvas = Canvas(root, width = 600, height = 600)
canvas.pack()
img = PhotoImage(file="pic.jpg")
canvas.create_image(20,20, anchor=NW, image=img)
root.mainloop()
实际上,我认为不可能直接将
jpg
与PhotoImage
一起使用,相反,您可能希望使用PIL
,下面是如何使用的在这之后,就说
Here is a site to convert jpg to gif
如果有任何错误或疑问,请务必让我知道
干杯
这可能是由于tkinter images出现了一个新的错误,我们必须保留一个对该图像的引用,以确保它能正常工作。我希望这会解决它
另外,正如@Cool Cloud也指出的,它有时不能处理jpg文件,但对我来说有时也会,所以如果它不能处理你,你可以将它转换成png或gif(根据我遇到这些问题时的处理方式,我更喜欢png)。 此外,您还可以尝试使用PIL库来代替转换,PIL库可以使用
然后您可以使用一个名为PhotoImage的类(是的,它与tkinter类同名)来进行图像加载和填充,它位于ImageTk模块下,可用于处理tkinter图像
您还可以将图像对象作为参数传递到此类中,您可以创建图像对象,如所示:
像这样
另外,如果您想了解更多关于PIL的信息,它基本上只是一个python图像处理库,通常也称为pillow
导入语句(以防不清楚)
相关问题 更多 >
编程相关推荐