Tkinter.PhotoImage 不支持 png 图像

2024-10-23 08:47:00
admin
原创
310
摘要:问题描述:我正在使用 Tkinter 编写 GUI 并希望在 中显示 png 文件Tkiner.Label。所以我有一些类似这样的代码:self.vcode.img = PhotoImage(data=open('test.png').read(), format=&...

问题描述:

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

self.vcode.img = PhotoImage(data=open('test.png').read(), format='png')
self.vcode.config(image=self.vcode.img)

这段代码在我的 Linux 机器上运行正常。但是当我在 Windows 机器上运行它时,它失败了。我也在其他几台机器上测试过(包括 Windows 和 Linux),它总是失败。

回溯是:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:Python27liblib-tkTkinter.py", line 1486, in __call__
    return self.func(*args)
  File "C:Documents and SettingsStclientGUI.py", line 150, in showrbox
    SignupBox(self, self.server)
  File "C:Documents and SettingsStclientGUI.py", line 197, in __init__
    self.refresh_vcode()
  File "C:Documents and SettingsStclientGUI.py", line 203, in refresh_vcode
    self.vcode.img = PhotoImage(data=open('test.png').read(), format='png')
  File "C:Python27liblib-tkTkinter.py", line 3323, in __init__
    Image.__init__(self, 'photo', name, cnf, master, **kw)
  File "C:Python27liblib-tkTkinter.py", line 3279, in __init__
   self.tk.call(('image', 'create', imgtype, name,) + options)
TclError: image format "png" is not supported

如果我format='png'在源代码中删除,回溯将变成:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:Python27liblib-tkTkinter.py", line 1486, in __call__
    return self.func(*args)
  File "C:Documents and SettingsStclientGUI.py", line 150, in showrbox
    SignupBox(self, self.server)
  File "C:Documents and SettingsStclientGUI.py", line 197, in __init__
    self.refresh_vcode()
  File "C:Documents and SettingsStclientGUI.py", line 203, in refresh_vcode
    self.vcode.img = PhotoImage(data=open('test.png').read())
  File "C:Python27liblib-tkTkinter.py", line 3323, in __init__
    Image.__init__(self, 'photo', name, cnf, master, **kw)
  File "C:Python27liblib-tkTkinter.py", line 3279, in __init__
    self.tk.call(('image', 'create', imgtype, name,) + options)
TclError: couldn't recognize image data

那么,我该怎么做才能让它支持 png 文件呢?


解决方案 1:

PIL 现已被 Pillow 取代http://pillow.readthedocs.io/en/3.2.x/

解决方案:

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在代码中找不到,则需要pillow安装:

pip install pillow

解决方案 2:

tkinter 仅支持 3 种文件格式,即 GIF、PGM 和 PPM。您要么需要将文件转换为 .GIF,然后加载它们(这要容易得多,但正如 jonrsharpe 所说,如果不先转换文件,什么都行不通),要么您可以将程序移植到 Python 2.7 并使用 Python 图像库 (PIL) 及其 tkinter 扩展来使用 PNG 图像。

您可能会发现有用的链接: http: //effbot.org/tkinterbook/photoimage.htm

解决方案 3:

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

如果您正在启动一个新项目,请不要使用接受答案中所建议的 python2 或 PIL,它们都是已贬值的技术。

解决方案 4:

已在适用于 OS X 的官方 python.org 64 位(仅限)安装程序中修复。Tk 版本 8.6 开箱即用。警告:如果您使用 homebrew,截至本文发布时,brew install python3您只能使用 8.5,而使用 png 需要 8.6,因此您必须使用官方安装程序。要检查您正在使用哪个 Tk:

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

如果报告 8.6,那么你就可以开始了。

解决方案 5:

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

解决方案 6:

在 Windows 上你必须使用这种特定的格式:

Example = PhotoImage(file='photo.png')

如果你想将其调整为较小的尺寸:

Example = Example.subsample(2, 2)

或者

Example = Example.subsample(3, 3)

总代码:

Example = PhotoImage(file='photo.png')
Example = Example.subsample(1, 1)

但未来警告,您必须将文件位置与照片一起归档,除非您将照片与脚本放在同一个文件中!

解决方案 7:

尝试使用 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:UserssaigopiDownloadslogo.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()

解决方案 8:

我使用 PhotoImage 为我的 Gui 添加了一个 png 格式的图标。如下所示,它可以工作或给你一个想法。

iconn = PhotoImage(file = "arcen.png" )
root.iconphoto(0, iconn)
相关推荐
  政府信创国产化的10大政策解读一、信创国产化的背景与意义信创国产化,即信息技术应用创新国产化,是当前中国信息技术领域的一个重要发展方向。其核心在于通过自主研发和创新,实现信息技术应用的自主可控,减少对外部技术的依赖,并规避潜在的技术制裁和风险。随着全球信息技术竞争的加剧,以及某些国家对中国在科技领域的打压,信创国产化显...
工程项目管理   1565  
  为什么项目管理通常仍然耗时且低效?您是否还在反复更新电子表格、淹没在便利贴中并参加每周更新会议?这确实是耗费时间和精力。借助软件工具的帮助,您可以一目了然地全面了解您的项目。如今,国内外有足够多优秀的项目管理软件可以帮助您掌控每个项目。什么是项目管理软件?项目管理软件是广泛行业用于项目规划、资源分配和调度的软件。它使项...
项目管理软件   1354  
  信创国产芯片作为信息技术创新的核心领域,对于推动国家自主可控生态建设具有至关重要的意义。在全球科技竞争日益激烈的背景下,实现信息技术的自主可控,摆脱对国外技术的依赖,已成为保障国家信息安全和产业可持续发展的关键。国产芯片作为信创产业的基石,其发展水平直接影响着整个信创生态的构建与完善。通过不断提升国产芯片的技术实力、产...
国产信创系统   21  
  信创生态建设旨在实现信息技术领域的自主创新和安全可控,涵盖了从硬件到软件的全产业链。随着数字化转型的加速,信创生态建设的重要性日益凸显,它不仅关乎国家的信息安全,更是推动产业升级和经济高质量发展的关键力量。然而,在推进信创生态建设的过程中,面临着诸多复杂且严峻的挑战,需要深入剖析并寻找切实可行的解决方案。技术创新难题技...
信创操作系统   27  
  信创产业作为国家信息技术创新发展的重要领域,对于保障国家信息安全、推动产业升级具有关键意义。而国产芯片作为信创产业的核心基石,其研发进展备受关注。在信创国产芯片的研发征程中,面临着诸多复杂且艰巨的难点,这些难点犹如一道道关卡,阻碍着国产芯片的快速发展。然而,科研人员和相关企业并未退缩,积极探索并提出了一系列切实可行的解...
国产化替代产品目录   28  
热门文章
项目管理软件有哪些?
云禅道AD
禅道项目管理软件

云端的项目管理软件

尊享禅道项目软件收费版功能

无需维护,随时随地协同办公

内置subversion和git源码管理

每天备份,随时转为私有部署

免费试用