Lupos
Lupos

Reputation: 906

Kivy Animation start Animation when a other Animation is complete

How Can i start in kivy a Animaztion when a other Animation is finish?I want to start the first Animation and when this animation is finish i want to start the next animation.

def on_button_clickedBIG(self):
        myLayout.CokkieAnzahl += 1

        print(myLayout.CokkieAnzahl)
        self.ids.CokkieZahl.text = str(myLayout.CokkieAnzahl)

        anim1 = Animation(size=(650,350), duration=0.3)
        anim1.start(self.ids.CookieImage)


    def on_button_clickedSMALL(self):

        anim2 = Animation(size=(600, 300))
        anim2.start(self.ids.CookieImage)

And here my kv language code:

 Image:
                size_hint_y: None
                size_hint_x: None
                keep_ratio: True
                allow_stretch: True
                id: CookieImage
                source: "cookie.png"
                on_touch_down: root.on_button_clickedBIG()
                #on_touch_down: root.on_button_clickedSMALL()
                size: 600, 300
                pos: -80, -30

EDIT1: I try it But i doesn´t work and so i ask if anybody know the way here is my solution how i try it but it doesn´t work:

anim1.bind(on_complete=myLayout.on_button_clickedSMALL)

This have i put in the def on_button_clickedBIG(self): But i get a error: TypeError: on_button_clickedSMALL() takes 1 positional argument but 2 were given

Upvotes: 0

Views: 682

Answers (2)

ikolim
ikolim

Reputation: 16011

You have to add the following into your program. I have also included an example for your reference.

  1. animation1.bind(on_complete=animation_complete)

  2. function animation_complete

Your Program

def on_button_clickedBIG(self):
    myLayout.CokkieAnzahl += 1

    print(myLayout.CokkieAnzahl)
    self.ids.CokkieZahl.text = str(myLayout.CokkieAnzahl)

    anim1 = Animation(size=(650,350), duration=0.3)
    anim1.bind(on_complete=self.animation_complete)
    anim1.start(self.ids.CookieImage)


def animation_complete(self, widget, item):
    self.remove_widget(item)
    self.on_button_clickedSMALL()


def on_button_clickedSMALL(self):

    anim2 = Animation(size=(600, 300))
    anim2.start(self.ids.CookieImage)

Program Example

animations.py

import kivy
kivy.require('1.10.0')

from kivy.app import App
from kivy.animation import Animation
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.button import Button
from kivy.properties import NumericProperty


class AnimateRecord(FloatLayout):

    angle = NumericProperty(0)

    def __init__(self, **kwargs):
        super(AnimateRecord, self).__init__(**kwargs)
        animation_record = Animation(angle=360, duration=2)
        animation_record += Animation(angle=360, duration=2)
        animation_record.repeat = True
        animation_record.start(self)

    def on_angle(self, item, angle):
        if angle == 360:
            item.angle = 0


class AnimateButton(FloatLayout):

    def animate_button(self, instance):
        # create an animation object. This object could be stored
        # and reused each call or reused across different widgets.
        # += is a sequential step, while &= is in parallel
        animation_btn = Animation(pos=(100, 100), t='out_bounce')
        animation_btn += Animation(pos=(200, 100), t='out_bounce')
        animation_btn &= Animation(size=(500, 500))
        animation_btn += Animation(size=(100, 50))

        animation_btn.bind(on_complete=self.animation_complete)

        # apply the animation on the button, passed in the "instance" argument
        # Notice that default 'click' animation (changing the button
        # color while the mouse is down) is unchanged.
        animation_btn.start(instance)

    def animation_complete(self, widget, item):
        self.remove_widget(item)
        self.add_widget(AnimateRecord())


class AnimationsApp(App):

    def build(self):
        layout = AnimateButton()
        # create a button, and  attach animate() method as a on_press handler
        button = Button(size_hint=(None, None), text='plop',
                        on_press=layout.animate_button)
        layout.add_widget(button)
        return layout


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

animations.kv

#:kivy 1.10.0

<AnimateRecord>:
    # Describe the record's position and size here
    size_hint: None, None
    size: 200, 200
    pos_hint: {'top': 0.7, 'right': 1.1}

    canvas.before:
        PushMatrix
        Rotate:
            angle: root.angle
            axis: (0, 0, 1)
            origin: root.center
    canvas.after:
        PopMatrix

    Image:
        source: '/home/iam/Pictures/Images/45rpm.png'
        size_hint: None, None
        # size: 200, 200
        size: self.parent.size  # So we don't have to change it every time, change the size above only
        pos_hint: {'center_x': 0.5, 'center_y': 0.5}

Upvotes: 1

Jerry the Beam
Jerry the Beam

Reputation: 21

The answer is in the Kivy Documentation: https://kivy.org/docs/api-kivy.animation.html try to search there next time before asking here.

Upvotes: 0

Related Questions