6

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!

Baxorr
  • 298
  • 6
  • 20
  • 1
    You could show your code, you could also explain what the following phrase means: *but without looping* – eyllanesc Nov 25 '17 at 00:30
  • 1
    I don't really see how any code would clarify the question, what i'm trying to learn is how to have a button animate from an off state to an on state when said button is pushed. To be more exact, here's a visual demonstration, [this](http://iceflowstudios.com/v3/wp-content/uploads/2012/01/GlowingPreview.gif) is very much like the gif i'd like to use. Without looping meaning that the gif should only play once, after that it should be a still image of the powered on button – Baxorr Nov 25 '17 at 01:38
  • You can do this with a custom image instead of a Button because in a button the gifs are just like simple other images – Simon Mengong Nov 25 '17 at 07:40

1 Answers1

9

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>

FJSevilla
  • 3,733
  • 1
  • 13
  • 20