栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用tkinter在python中播放动画GIF

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用tkinter在python中播放动画GIF

该错误表示您尝试加载100帧,但是gif小于此数量。

众所周知,tkinter中的gif动画效果很差。我在很久以前就编写了这段代码,您可以从中窃取,但是除了小gif之外,其他任何东西都不会让您感到la懒:

import tkinter as tkfrom PIL import Image, ImageTkfrom itertools import countclass ImageLabel(tk.Label):    """a label that displays images, and plays them if they are gifs"""    def load(self, im):        if isinstance(im, str): im = Image.open(im)        self.loc = 0        self.frames = []        try: for i in count(1):     self.frames.append(ImageTk.PhotoImage(im.copy()))     im.seek(i)        except EOFError: pass        try: self.delay = im.info['duration']        except: self.delay = 100        if len(self.frames) == 1: self.config(image=self.frames[0])        else: self.next_frame()    def unload(self):        self.config(image="")        self.frames = None    def next_frame(self):        if self.frames: self.loc += 1 self.loc %= len(self.frames) self.config(image=self.frames[self.loc]) self.after(self.delay, self.next_frame)root = tk.Tk()lbl = ImageLabel(root)lbl.pack()lbl.load('ball-1.gif')root.mainloop()


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/624025.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号