动画在Kivy的同时

时间:2015-06-20 18:38:22

标签: python python-2.7 kivy

我正在编写一个简单的视错觉应用程序来学习如何在Kivy中编码。我想知道为什么我的程序崩溃以及如何解决它。 如果我取消注释animation.start(self.c4)行,那么我的程序运行良好,所以我几乎可以确定问题是两个动画同时运行。错误是什么以及如何解决?

我的.py文件:

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()

我的.kv文件:

<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

1 个答案:

答案 0 :(得分:2)

我认为问题可能在于每次调用Animation函数时重新创建新的on_complete

尝试使用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)