Python Tkinter.PhotoImage不';不支持png图像

Python Tkinter.PhotoImage不';不支持png图像,python,linux,tkinter,tk,Python,Linux,Tkinter,Tk,我正在使用Tkinter编写GUI,并希望在Tkiner.Label中显示png文件。 所以我有一些类似这样的代码: self.vcode.img = PhotoImage(data=open('test.png').read(), format='png') self.vcode.config(image=self.vcode.img) 此代码在我的Linux机器上正确运行。但是当我在我的windows机器上运行它时,它失败了。我也在其他几台机器上测试过(包括windows和linux),它

我正在使用Tkinter编写GUI,并希望在
Tkiner.Label
中显示png文件。 所以我有一些类似这样的代码:

self.vcode.img = PhotoImage(data=open('test.png').read(), format='png')
self.vcode.config(image=self.vcode.img)
此代码在我的Linux机器上正确运行。但是当我在我的windows机器上运行它时,它失败了。我也在其他几台机器上测试过(包括windows和linux),它总是失败

回溯是:

Tkinter回调中出现异常 回溯(最近一次呼叫最后一次): 文件“C:\Python27\lib\lib tk\Tkinter.py”,第1486行,在uu调用中__ 返回self.func(*args) showrbox中第150行的文件“C:\Documents and Settings\St\client\GUI.py” SignupBox(self,self.server) 文件“C:\Documents and Settings\St\client\GUI.py”,第197行,在\uuu init中__ self.refresh_vcode() 文件“C:\Documents and Settings\St\client\GUI.py”,第203行,在刷新\u vcode中 self.vcode.img=PhotoImage(data=open('test.png').read(),format='png')) 文件“C:\Python27\lib\lib tk\Tkinter.py”,第3323行,在uu init中__ 图像。_u初始(自我,“照片”,名称,cnf,主机,**千瓦) 文件“C:\Python27\lib\lib tk\Tkinter.py”,第3279行,在uu init中__ self.tk.call(('image','create',imgtype,name,)+选项) 错误:不支持图像格式“png” 如果我在源代码中删除
format='png'
,回溯将变成:

Tkinter回调中出现异常 回溯(最近一次呼叫最后一次): 文件“C:\Python27\lib\lib tk\Tkinter.py”,第1486行,在uu调用中__ 返回self.func(*args) showrbox中第150行的文件“C:\Documents and Settings\St\client\GUI.py” SignupBox(self,self.server) 文件“C:\Documents and Settings\St\client\GUI.py”,第197行,在\uuu init中__ self.refresh_vcode() 文件“C:\Documents and Settings\St\client\GUI.py”,第203行,在刷新\u vcode中 self.vcode.img=PhotoImage(data=open('test.png').read()) 文件“C:\Python27\lib\lib tk\Tkinter.py”,第3323行,在uu init中__ 图像。_u初始(自我,“照片”,名称,cnf,主机,**千瓦) 文件“C:\Python27\lib\lib tk\Tkinter.py”,第3279行,在uu init中__ self.tk.call(('image','create',imgtype,name,)+选项) 错误:无法识别图像数据
那么,我应该怎么做才能使它支持png文件呢?

tkinter只支持3种现成的文件格式,即GIF、PGM和PPM。您要么需要将文件转换为.GIF,然后加载它们(要容易得多,但正如Jornsharpe所说,如果不先转换文件,任何事情都无法进行),要么可以将程序移植到Python 2.7,并使用Python图像库(PIL)及其tkinter扩展名来使用PNG图像


您可能会发现一个有用的链接:

Tkinter 8.6支持png文件格式,而Tkinter 8.5不支持。如果您有升级python的选项,并且可以使用png。 如果您必须使用较旧版本的python,那么应该使用Pillow(维护的pil fork),它也适用于python3


如果您正在启动一个新项目,请不要按照公认答案中的建议使用python2或PIL,它们都是折旧技术

PIL现在被枕头取代

解决方案:

from Tkinter import *
import PIL.Image
import PIL.ImageTk

root = Toplevel()

im = PIL.Image.open("photo.png")
photo = PIL.ImageTk.PhotoImage(im)

label = Label(root, image=photo)
label.image = photo  # keep a reference!
label.pack()

root.mainloop()

如果在代码中找不到
PIL
,您确实需要
枕头
安装:

pip install pillow

尝试使用PIL库,而不是将图像转换为GIF、PGM或PPM(PhotoImage),仅接受这3种格式

import tkinter as tk
import PIL.Image
import PIL.ImageTk

base = tk.Tk()
base.title("Dialy Dose")

logoPath = r"C:\Users\saigopi\Downloads\logo.png"

ref = PIL.Image.open(logoPath)
photo = PIL.ImageTk.PhotoImage(im)

inputEdit = tk.Label(base,text="Enter Quote")
save = tk.Button(base,text="Save",background="green",command=save())
logo = tk.Label(base,image=photo,text="Logo bro lite")
quote = tk.Label(base,text="I am saying you are more than something")

inputEdit.pack()
save.pack()
logo.pack()
quote.pack()

base.mainloop()

在官方python.org中修复了OS X的64位(仅限)安装程序。Tk 8.6版是现成的。警告:如果您使用自制软件,从本帖开始,执行
brew安装python3
将只提供8.5版本,使用png需要8.6版本,因此您必须使用官方安装程序。要检查您正在使用的Tk,请执行以下操作:

$ python3 -c 'import tkinter; print(tkinter.TkVersion);'

如果它报告了8.6,您就可以开始了。

您是否尝试过使用不同的文件格式?请参阅,例如…@jornsharpe,是的,我尝试了“gif”格式,它在我的linux计算机上显示图像,但在我的windows计算机上显示黑色(或白色或一些噪声)区域。您使用了什么转换图像?它在
Tk
应用程序之外的所有机器上都能正确显示吗?@jornsharpe,我不使用任何东西来转换图像,它只是从互联网下载的图像。当然,我测试了其他几个图像,都失败了。图像可以在
Tk
应用程序外正确显示(在windows上使用“windows图像和传真查看器”,在linux上使用“feh”)。那么你只是告诉tkinter它是gif,而实际上仍然是png?你为什么认为那会有用?!转换一下,谢谢。我很抱歉,我没有表达清楚。我的意思是,我尝试了使用另一个gif图像的“gif”格式,而不是“png”格式,它在我的linux机器上正确地显示了图像,但在我的windows机器上显示了一个黑色(或白色或有时有噪音)区域,正如我对jonrsharpe所说的。最后,我发现如果我使用
self.vcode.img=PhotoImage(data=open('test.gif').read(),format='gif')
,它会失败。但是如果我使用
self.vcode.img=PhotoImage(file='test.gif')
tk 8.6 supports.png,它就可以正常工作。Widows 3.4.z安装程序在*nix上安装tcl/tk 8.6.z,这取决于系统附带的内容以及用户升级到的内容。这就是为什么你的程序可以在一台机器和另一台机器上工作。OSX将有8.5.z版本,除非用户以某种方式升级。如果可能的话,OSX的PSF3.5安装程序将安装8.6。@TerryJanReedy这对我来说是个新闻,但我很高兴听到这个消息。我会记住这一点以备将来参考。@TerryJanReedy感谢上帝我读了你的评论,这可能为我节省了很多时间。更正:PSF 3.6(而不是3.5)安装程序有望安装tk 8.6。对于3.5,可以使用不同的安装程序,如“自制”中的安装程序。我不在OSX上,也不知道其他Python Mac安装程序的优缺点。这个问题不涉及PIL的使用,所以不清楚你认为这与这个问题有什么关系。我不明白。您想完全重写PIL以使其可用吗?我只是提供了一个最简单的解决方案。这个问题不是一个简单的问题
from tkinter import *
from tkinter import messagebox
import os
from PIL import Image, ImageTk


root = Tk()
root.geometry("1300x720")
root.title("KEDİLERİMİZ ve KÖPEKLERİMİZ")
class Ana:
    def __init__(self,name,roll):
        self.name = name
        self.roll = roll
resim = Label(root,width=77,height=43,bg="blue")
resim.place(x=730,y=10)
o = "1.PNG"
hu = o.find(".")
mu = o[hu:]
if mu == ".gif" or mu == ".png":
    img = PhotoImage(file = o)
else:
    photo = Image.open(o)
    img = ImageTk.PhotoImage(photo)
resim.configure(image=img,width=img.width(),height=img.height())
resim.image = img