【问题标题】:Gif image not working in tkinterGif图像在tkinter中不起作用
【发布时间】:2017-10-30 20:56:06
【问题描述】:
 from tkinter import * 
 photo = PhotoImage(file="C:\Temp\test\computer.gif")
 lbl = Label(root, image=photo, height="10", width="20").pack

我完全不知道为什么这不起作用:_tkinter.TclError: couldn't recognize data in image file "C:\Temp\test\computer.gif.

【问题讨论】:

  • 请记住,在上面的代码中,您的lbl 不是一个标签对象,而是一个函数对象。如果你想调用pack方法,你需要在一个单独的行中进行。

标签: python windows tkinter gif


【解决方案1】:

Windows 文件名总是必须作为原始字符串输入(在所有 python 中,而不仅仅是在 tkinter 中)。此外,您必须先创建一个根窗口。试试这个:

from tkinter import * 
root = Tk()
photo = PhotoImage(file=r"C:\Temp\test\computer.gif")
Label(root, image=photo, height="10", width="20").pack()
root.mainloop()

【讨论】:

  • 我有 root = tk() 和 root.mainloop 在我忘记把它放在帖子之前。当我添加 r 时,它仍然出现同样的错误。
  • 您确定文件没有损坏吗?是否适用于其他文件?
  • lbl = None 也在这里。
猜你喜欢
  • 2015-02-15
  • 2013-10-17
  • 1970-01-01
  • 1970-01-01
  • 2016-08-28
  • 2019-04-29
  • 2023-02-25
  • 2018-11-04
  • 1970-01-01
相关资源
最近更新 更多