Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python tkinter代码为一些GIF设置动画,但不是全部GIF_Python_Tkinter_Gif - Fatal编程技术网

Python tkinter代码为一些GIF设置动画,但不是全部GIF

Python tkinter代码为一些GIF设置动画,但不是全部GIF,python,tkinter,gif,Python,Tkinter,Gif,我使用以下代码在tkinter上设置GIF动画 class gif: def __init__(self,filename): fIndex = 0 global frames frames = [] global lastFrame while True:#infinite loop try:#executes following code if possible

我使用以下代码在tkinter上设置GIF动画

class gif:
    def __init__(self,filename):
        fIndex = 0
        global frames
        frames = []
        global lastFrame
        while True:#infinite loop
            try:#executes following code if possible
                part = 'gif -index {}'.format(fIndex)#takes an index of a frame of gif
                frame = PhotoImage(file=filename,format=part)
                print(str(fIndex))
        #converts each frame to a PhotoImage
            except:#once the last frame is reached, this is executed
                lastFrame = fIndex - 1#last frame index saved
                break #while loop ends
            frames.append(frame) #photoimage of frame added to list
            fIndex += 1 #index incremented


    def animate(self,fNumber):#function to animate gif
        if fNumber > lastFrame: #if the last frame is reached,
            fNumber = 0 #index changed to 0 so it plays again
        canvas.create_image(700,350,image=frames[fNumber],anchor=CENTER) 
        master.after(20,self.animate,fNumber+1)#each frame played with
    #20 milliseconds in between

matr1x = gif('spinningmatrix.gif')
matr1x.animate(0)
这对我的一些GIF有效,但不是全部。对于那些不起作用的,当我打印fIndex时,它只显示0和1,GUI上只显示第一帧。有效的索引会显示更多的索引。只是想知道为什么有些不起作用,为什么它只给我0和1作为索引。任何帮助都将不胜感激。
编辑:我在线将GIF分割成帧,对于2个正常工作的GIF,我的程序给出了正确的帧数。对于一个可以工作但有点差的,程序显示3帧,但在线显示20帧。对于其他两个根本不起作用的图像,在线图像的帧数差别很大。

听起来有些GIF没有多个帧,和/或所有帧都是相同的。“画布。创建图像(…”:您正在一次又一次地创建和堆叠图像。请继续阅读