user4850501
user4850501

Reputation:

Animations in the same time in Kivy

I am coding a simple optical illusion app to learn how to code in Kivy. I would like to know why my program crashes and how to solve it. If I uncomment the animation.start(self.c4) line then my program runs well so I'm almost sure that the problem is two animations are running in the same time. What is the error and how to solve it?

My .py file:

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import NumericProperty, ObjectProperty,\
ReferenceListProperty
from kivy.graphics import Color
from kivy.core.window import Window
from kivy.animation import Animation

class Circle(Widget):
    v = NumericProperty(0)

class Illusion(Widget):
    c1 = ObjectProperty(None)
    c2 = ObjectProperty(None)
    c3 = ObjectProperty(None)
    c4 = ObjectProperty(None)

    def __init__(self, **kwargs):
        super(Illusion, self).__init__(**kwargs)

        objects = (self.c1, self.c2, self.c3, self.c4)
        values = (0,1,0,0)

        for i, item in enumerate(objects):
            objects[i].v = values[i]

        self.start_illusion(instance = None, value = None)

    def start_illusion(self, instance, value):
        animation = Animation(v = 1, d = .25)
        animation.bind(on_complete=self.continue_illusion)
        animation.start(self.c3)
        animation.start(self.c4)

    def continue_illusion(self, instance, value):
        animation = Animation(v = 0, d = .25)
        animation.bind(on_complete=self.start_illusion)
        animation.start(self.c3)
        animation.start(self.c4)

class IllusionsApp(App):
    Window.clearcolor = (0,0,1,1)
    def build(self):
        return Illusion()

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

My .kv file:

<Circle>:
    canvas:
        Color:
            hsv: 0,0,self.v
        Ellipse:
            pos: self.pos
            size: self.size

<Illusion>:
    c1: _c1
    c2: _c2
    c3: _c3
    c4: _c4

    Circle:
        id: _c1
        size: 250,250
        center_x: self.parent.width / 4
        center_y: self.parent.height / 2

    Circle:
        id: _c2
        size: 250,250
        center_x: self.parent.width * 3/4
        center_y: self.parent.height / 2

    Circle:
        id: _c3
        size: 180,180
        center_x: self.parent.width / 4
        center_y: self.parent.height / 2

    Circle:
        size: 180,180
        id: _c4
        center_x: self.parent.width * 3/4
        center_y: self.parent.height / 2

Upvotes: 0

Views: 933

Answers (1)

ThePavolC
ThePavolC

Reputation: 1738

I think the problem might be with recreating new Animation with each call of on_complete functions.

Try to use repeat animation property.

def __init__(self, **kwargs):
        super(Illusion, self).__init__(**kwargs)

        objects = (self.c1, self.c2, self.c3, self.c4)
        values = (0,1,0,0)

        for i, item in enumerate(objects):
            objects[i].v = values[i]

        self.animation_v0 = Animation(v = 0, d = .25) + Animation(v = 1, d = .25)
        self.animation_v0.repeat = True
        self.animation_v1 = Animation(v = 1, d = .25) + Animation(v = 0, d = .25)
        self.animation_v1.repeat = True

        #self.animation_v0.bind(on_complete=self.start_illusion)
        #self.animation_v1.bind(on_complete=self.continue_illusion)

        self.animation_v0.start(self.c3)
        self.animation_v1.start(self.c4)

    def start_illusion(self, instance, value):
        self.animation_v0.start(self.c3)

    def continue_illusion(self, instance, value):
        self.animation_v1.start(self.c4)

Upvotes: 1

Related Questions