【问题标题】:Tkinter: Image not recognisedTkinter:图像无法识别
【发布时间】:2014-07-13 16:16:26
【问题描述】:

我正在使用 Tkinter 构建一个简单的 GUI 程序。我想要一堆带有应用程序图像的按钮,单击时启动该应用程序。问题是,Python 无法识别Skype.gif 文件。

代码:

from tkinter import *
import os

def open_skype():
    os.system('open /Applications/Skype.app')

master = Tk()

photo = PhotoImage(file='/Users/michael/Desktop/Skype.gif')

but = Button(master, image=photo, command=open_skype)



objs = [but]

column = 1
for i in objs:
    i.grid(column=column, row=1)
    column += 1
mainloop()

错误信息:

_tkinter.TclError: 无法识别图像文件“/Users/michael/Desktop/Skype.gif”中的数据

【问题讨论】:

  • 使用PNGJPG格式的图片。
  • 我尝试了这些,我得到了同样的错误:_tkinter.TclError:无法识别图像文件“/Users/michael/Desktop/Skype.png”中的数据
  • 您是如何将其从 GIF 转换为 PNG 的?你能在一些图像查看器中看到这张图片吗?
  • 肯定转换过,没有损坏。
  • 你能在某些图片查看器中看到这张图片吗?

标签: python tkinter


【解决方案1】:

您的问题很可能是图片位置不正确。要确保它是,请尝试输入您的命令行(Mac 的终端),然后输入ls /Users/michael/Desktop/Skype.gif。如果打印Skype.gif/Users/michael/Desktop/Skype.gif,则文件存在,否则不存在。

【讨论】:

  • 不,这不是问题 - 我确保路径是正确的。
【解决方案2】:

路径从 python 文件所在的位置开始,所以如果它们在同一个文件夹中,只需输入photo = PhotoImage(file='Skype.gif')

【讨论】:

    【解决方案3】:

    Tkinter 在某些情况下只能识别 s PNG 图像或 JPG 图像。如果你有一个 gif 图像,你可以使用另一个名为 WxPython 的模块来合并它。它的工作原理是它使用逐帧图像来显示视频类型的图像。 Tkinter 不支持这个。

    【讨论】:

      【解决方案4】:

      确保您的图片格式为 PNG 或 JPG

      【讨论】:

        猜你喜欢
        • 2021-10-18
        • 2015-04-28
        • 2019-07-29
        • 1970-01-01
        • 1970-01-01
        • 2018-05-01
        • 1970-01-01
        • 2021-03-30
        • 2015-06-14
        相关资源
        最近更新 更多