Search code examples
pythonkivygifanimated-gif

Kivy Gif animation runs too often


I wanted to make a kivy program with a gif animation which runs once and then stops. I set anim_loop to 1 , but it keeps running over and over again. Here is the code:

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`

Solution

  • anim_loop property should work in Kivy 1.9.0, if you are using an older version then consider an upgrade.

    There is also an another way. You can stop an animation using following code:

    myimage._coreimage.anim_reset(False)
    

    To stop an animation after it was played once observe the texture propery. It will be changed after loading each of the frames. If your GIF have n frames then stop the animation after (n+1)-th call of the on_texture method.

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