在Kivy on_complete动画事件中使用递归? [英] using recursion with Kivy on_complete Animation event?

查看:85
本文介绍了在Kivy on_complete动画事件中使用递归?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试按顺序对10个小部件进行动画处理,每个动画应在上一个完成时开始.不幸的是,我下面的解决方案会按顺序触发所有动画,而无需等待任何操作完成.

I am attempting to animate 10 widgets in sequence, each animation should start as the previous completes. Unfortunately my solution below fires all the animations in sequence without waiting for anything to complete.

def Animate_Widget(self,index):
    if index < 10:
        anim = Animation(x = self.position[index][0], y = self.position[index][1], d=1)
        anim.bind(on_complete=self.Animate_Widget(index + 1))
        anim.start(self.ids['widget' + str(index)])

def Resize_Layout(self):
    self.Animate_Widget(0)

推荐答案

此处:

anim.bind(on_complete=self.Animate_Widget(index + 1))

您立即拨打回叫电话.这是因为anim.bind的参数是在调用时求值的.而是定义另一个函数或lambda来延迟可以不带任何参数调用的调用:

You call your callback straight away. This is because the arguments to anim.bind are evaluated when calling. Instead, define another function or lambda to delay calling that can be called without any arguments:

def callback():
    self.Animate_Widget(index + 1)
anim.bind(on_complete=callback)

或者:

anim.bind(on_complete=lambda: self.Animate_Widget(index + 1))

这篇关于在Kivy on_complete动画事件中使用递归?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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