Kivy中同一时间的动画 [英] Animations in the same time in Kivy

查看:78
本文介绍了Kivy中同一时间的动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

我的.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

推荐答案

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

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

尝试使用重复动画属性.

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)

这篇关于Kivy中同一时间的动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆