我尝试了两种不同的方法来让图片在标签上显示出来
#This gives " TclError: couldn't recognize data in image file "TestImage.gif" "
imgPath = "TestImage.gif"
photo = PhotoImage(file = imgPath)
label = Label(image = photo)
label.image = photo # keep a reference!
label.grid(row = 3, column = 1, padx = 5, pady = 5)
以及
#This gives no error but the image doesn't show
imgPath = "TestImage.gif"
photo = PhotoImage(imgPath)
label = Label(image = photo)
label.image = photo # keep a reference!
label.grid(row = 3, column = 1, padx = 5, pady = 5)
图像与所有代码位于同一文件夹中。关于如何展示形象有什么建议吗?
Bryan Oakley是正确的,尽管文件系统认为图像是gif,但就其内容而言,图像不是jpg。
最后我试着用你的程序打开一个jpg,得到了同样的错误'TclError:couldnotrecogneddatainimagefile'hello.jpg'
因此您可以这样做:使用mspaint打开您的图像,然后转到“文件”>;“另存为”,然后从“另存为类型”下拉列表中选择GIF。那么代码就可以工作了。这是我用过的:
(顺便说一下,如果我将上面的第7行改为
photo = PhotoImage(imgPath)
,那么和您一样,不会出现图像。所以把它留作photo = PhotoImage(file = imgPath)
)相关问题 更多 >
编程相关推荐