鱼C论坛

 找回密码
 立即注册
查看: 3832|回复: 7

[技术交流] Python 小技巧 065:Python 显示 GIF 动图

[复制链接]
发表于 2020-2-15 21:14:13 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
Python 显示 GIF 动图


先上代码(请先安装 Pyglet 第三方库):

  1. import pyglet

  2. sprite = pyglet.sprite.Sprite(pyglet.resource.animation("1.gif"))    # 选择当前工作目录下的一张图片
  3. win = pyglet.window.Window(width=sprite.width, height=sprite.height)


  4. @win.event
  5. def on_draw():
  6.     win.clear()
  7.     sprite.draw()


  8. pyglet.app.run()
复制代码


运行这段代码,在你工作目录下的 1.gif 就显示出来了

20200215_211059.gif

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-2-15 21:30:50 | 显示全部楼层
想要gif图片
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-15 21:31:25 | 显示全部楼层

好吧~

1.gif
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-15 21:36:46 | 显示全部楼层

嘿嘿嘿
感谢!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-16 17:18:50 | 显示全部楼层

不行呀,不支持 3.6.6 吗:

  1. Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
  2. Type "copyright", "credits" or "license()" for more information.
  3. >>>
  4. ================== RESTART: C:\Users\Angel\Desktop\test.py ==================
  5. Traceback (most recent call last):
  6.   File "C:\Users\Angel\AppData\Local\Programs\Python\Python36\lib\site-packages\pyglet\resource.py", line 607, in animation
  7.     identity = self._cached_animations[name]
  8.   File "C:\Users\Angel\AppData\Local\Programs\Python\Python36\lib\weakref.py", line 137, in __getitem__
  9.     o = self.data[key]()
  10. KeyError: '1.gif'

  11. During handling of the above exception, another exception occurred:

  12. Traceback (most recent call last):
  13.   File "C:\Users\Angel\AppData\Local\Programs\Python\Python36\lib\site-packages\pyglet\resource.py", line 460, in file
  14.     location = self._index[name]
  15. KeyError: '1.gif'

  16. During handling of the above exception, another exception occurred:

  17. Traceback (most recent call last):
  18.   File "C:\Users\Angel\Desktop\test.py", line 3, in <module>
  19.     sprite = pyglet.sprite.Sprite(pyglet.resource.animation("1.gif"))    # 选择当前工作目录下的一张图片
  20.   File "C:\Users\Angel\AppData\Local\Programs\Python\Python36\lib\site-packages\pyglet\resource.py", line 609, in animation
  21.     animation = pyglet.image.load_animation(name, self.file(name))
  22.   File "C:\Users\Angel\AppData\Local\Programs\Python\Python36\lib\site-packages\pyglet\resource.py", line 463, in file
  23.     raise ResourceNotFoundException(name)
  24. pyglet.resource.ResourceNotFoundException: Resource "1.gif" was not found on the path.  Ensure that the filename has the correct captialisation.
  25. >>>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-16 17:19:33 | 显示全部楼层
一个账号 发表于 2020-2-16 17:18
不行呀,不支持 3.6.6 吗:

不要用 IDLE 运行
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-16 17:21:14 | 显示全部楼层
zltzlt 发表于 2020-2-16 17:19
不要用 IDLE 运行

还是不行呀:

1.png
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-16 17:23:59 | 显示全部楼层

1.gif 存在吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-26 10:36

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表