Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 830
Baxorr Avatar asked Nov 25 '17 00:11

Baxorr


People also ask

Can a GIF be clickable?

I don't think a GIF can have a clickable link (like a specific button or text to click on), but the GIF itself can be clickable. That would be set in HTML when putting the GIF on a website/email, the same as making a button or other image (PNG, JPG, etc.) clickable.

How do you play an animated GIF?

To play animated GIF files, you must open the files in the Preview/Properties window. To do this, select the animated GIF file, and then on the View menu, click Preview/Properties. If the GIF does not play, try re-saving the animated GIF in the collection in which you want to put it.

How do I make a GIF run automatically?

You can use a free extension called Google GIFs Chrome Extension or Google gifs autoplay to do that. This extension works silently in the background and plays all animated GIFs in the Google image search results.


1 Answers

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>

like image 52
FJSevilla Avatar answered Sep 18 '22 14:09

FJSevilla