首页 > 解决方案 > python:kivy动画计数执行

问题描述

编程新手,我似乎无法计算动画执行了多少次,我希望能够在动画执行/完成特定次数后关闭它。

类 MainApp(App):计数 = 0

def build(self):
    return Builder.load_file("kv_ellipse.kv")

def on_start(self):
    print("start")
    Clock.schedule_interval(self.animate_the_widget, 1.1)

def animate_the_widget(self, widget, *args):

    self.count + 1
    print("animate")
    widget = self.root.ids.the_blinking_button
    anim = Animation(animated_color=(0, 0, .5, 0), blink_size=200)
    anim.bind(on_complete = self.counter)
    anim.start(widget)

def reset(self, *args):
    print("reset")
    widget = args[1]
    widget.animated_color = (.1, .4, .4, 1)
    widget.blink_size = 5

def counter(self, *args):
    self.reset(self.count + 1)
    print('counter', self.count)

MainApp().run()

标签: python-3.xanimationkivy

解决方案


首先将时钟计划设置为类属性,以便稍后取消计划。

self.clock = Clock.schedule_interval(self.animate_the_widget, 1.1)

然后,当你在 python 中增加一个计数器时,这样做。

self.count += 1

然后稍后您可以检查此计数器并取消计划。

if self.count > 9:
    Clock.unschedule(self.clock)
    # and / or do whatever you need here. 
    #If it's just to stop the app, exit() is enough.
    exit()

推荐阅读