Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在tkinter窗口中显示图像错误-无法';无法识别数据错误_Python_Python 3.x_User Interface_Tkinter - Fatal编程技术网

Python 在tkinter窗口中显示图像错误-无法';无法识别数据错误

Python 在tkinter窗口中显示图像错误-无法';无法识别数据错误,python,python-3.x,user-interface,tkinter,Python,Python 3.x,User Interface,Tkinter,语言:Python 你好 这是我的密码 我得到的错误- 回溯(最近一次呼叫最后一次): 文件“d:/development/Python/TheNewBoston/Python/GUI/1/script6.py”,第8行,在 photo=PhotoImage(file=“duck.jpg”) 文件“C:\Users\hirusha\AppData\Local\Programs\Python\Python38\lib\tkinter\\ uuuuuu init\uuuuu.py”,第4061行,在

语言:Python

你好

这是我的密码 我得到的错误-
回溯(最近一次呼叫最后一次):
文件“d:/development/Python/TheNewBoston/Python/GUI/1/script6.py”,第8行,在
photo=PhotoImage(file=“duck.jpg”)
文件“C:\Users\hirusha\AppData\Local\Programs\Python\Python38\lib\tkinter\\ uuuuuu init\uuuuu.py”,第4061行,在\uuu init中__
图像。_u初始(自我,“照片”,名称,cnf,主机,**千瓦)
文件“C:\Users\hirusha\AppData\Local\Programs\Python\Python38\lib\tkinter\\ uuuuuu init\uuuuuu.py”,第4006行,在\uuu init中__
self.tk.call(('image','create',imgtype,name,)+选项)
_tkinter.TclError:无法识别图像文件“duck.jpg”中的数据
链接到我下载的图像-

我不想做什么- 我只是想在我的程序中显示鸭子的图像! 像这样-

我是python新手!我知道如何读一些简单的错误。。但是我不明白他们在这里说什么


非常感谢您的帮助,谢谢

tkinter.PhotoImage不支持“.jpg”

所以我们可以使用“枕头”模式来实现这一点 第一: 通过安装枕头
pip安装枕头
在窗户里

然后您可以尝试:

from tkinter import *
from PIL import ImageTk, Image
root = Tk()

canv = Canvas(root, width=800, height=800)
canv.grid(row=2, column=3)

img = ImageTk.PhotoImage(Image.open("duck.jpg"))  # PIL solution
canv.create_image(20, 20, anchor=NW, image=img)

mainloop()

这对我有用

我建议您尝试使用.png图像。TKinter在过去的版本中遇到了.jpg问题。您下载的文件不是JPEG图像,而是一个文件。Tk无法识别此格式(或扩展名错误)。我把它转换成了jpg格式,就像这样。我可以确认,一旦转换成PNG格式,应用程序就可以工作了。对于JPEG格式,它不起作用。@HirushaAdikari下次我建议使用打开图像,然后使用显示图像
PIL
支持多种文件格式并支持多种图像处理。是的,它起到了作用。。你能给我解释一下吗。。。这是我的输出:PhotoImage仅接受GIF、PPM/PGM格式
Traceback (most recent call last):
  File "d:/development/Python/TheNewBoston/Python/GUI/1/script6.py", line 8, in <module>
    photo = PhotoImage(file="duck.jpg")
  File "C:\Users\hirusha\AppData\Local\Programs\Python\Python38\lib\tkinter\__init__.py", line 4061, in __init__
    Image.__init__(self, 'photo', name, cnf, master, **kw)
  File "C:\Users\hirusha\AppData\Local\Programs\Python\Python38\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 "duck.jpg"
from tkinter import *
from PIL import ImageTk, Image
root = Tk()

canv = Canvas(root, width=800, height=800)
canv.grid(row=2, column=3)

img = ImageTk.PhotoImage(Image.open("duck.jpg"))  # PIL solution
canv.create_image(20, 20, anchor=NW, image=img)

mainloop()