Gif 图像在 tkinter 中不起作用

Gif image not working in tkinter

 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.

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

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()