Kivy Gif 动画运行过于频繁

Kivy Gif animation runs too often

我想制作一个带有 gif 动画的 kivy 程序,它运行一次然后停止。 我将 anim_loop 设置为 1 ,但它会一遍又一遍地保持 运行 。 这是代码:

Root = Builder.load_string('''
  Image:
    source: 'streifen1.gif'
    size_hint_y: 1
    anim_loop: 1
''')


class TTT(App):
 def build(self):
    Window.clearcolor = (0, 0.5, 1, 1)# sets the backgroundcolor
    return Root #returnst the kv string and therefore the root widget`

anim_loop 属性 应该可以在 Kivy 1.9.0 中使用,如果您使用的是旧版本,请考虑升级。

还有一个办法。您可以使用以下代码停止动画:

myimage._coreimage.anim_reset(False)

要在动画播放一次后停止观察 texture 属性。它将在加载每个帧后更改。如果您的 GIF 有 n 帧,则在第 (n+1) 次调用 on_texture 方法后停止动画。

from kivy.app import App
from kivy.uix.image import Image

class MyImage(Image):
    frame_counter = 0
    frame_number = 2 # my example GIF had 2 frames

    def on_texture(self, instance, value):     
        if self.frame_counter == self.frame_number + 1:
            self._coreimage.anim_reset(False)
        self.frame_counter += 1


class MyApp(App):
    def build(self):
        return MyImage(source = "test.gif")

if __name__ == '__main__':
    MyApp().run()