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

查看:19
本文介绍了在 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天全站免登陆