Baxorr
Baxorr

Reputation: 318

How can I use an animated gif that will play when pressed as a button in kivy?

I have this toggle button in kivy and I want it to animate on when pressed (it's a power on button gif) but without looping. I can't seem to find any useful information about this. Any help is appreciated, thanks!

Upvotes: 6

Views: 9930

Answers (1)

FJSevilla
FJSevilla

Reputation: 4513

Using an instance of kivy.uix.image inside the button you can do:

  • Disable animation at startup anim_delay = -1.

  • Specify the number of loops to be played using anim_loop = 1

  • When the button is pressed, assign a positive value to anim_delay and restart animation using the anim_reset method of the kivy.core.image instance used by kivy.uix.image to contain the image.


from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder

Builder.load_string("""
<ExampleApp>:
    orientation: "vertical"
    Button:
        text: ""
        on_press: gif.anim_delay = 0.10
        on_press: gif._coreimage.anim_reset(True)

        Image:
            id: gif
            source: 'img.gif'
            center: self.parent.center
            size: 500, 500
            allow_stretch: True
            anim_delay: -1
            anim_loop: 1
""")

class ExampleApp(App, BoxLayout):
    def build(self):
        return self

if __name__ == "__main__":
    ExampleApp().run() 

<code>enter image description here</code>

Upvotes: 9

Related Questions