You need to enable JavaScript to run this app. Python Kivy框架如何实现Clock调度倒计时归0后自动重置为60秒
优惠活动
大模型
产品
解决方案
定价
更多

Python Kivy框架如何实现Clock调度倒计时归0后自动重置为60秒

解决方案

只需要在倒计时结束触发的stop_interval方法中添加重置计时器文本的逻辑即可,修改后的Begin1After类代码如下:

class Begin1After(MDFloatLayout):

    def timer_sec(self, *args):
        self.function_interval = Clock.schedule_interval(self.update_label, 1)
        Clock.schedule_once(self.stop_interval, 59.5)

    def stop_interval(self, *args):
        self.function_interval.cancel()
        # 倒计时结束后自动重置为60秒
        self.ids.timeri.text = "60"

    def update_label(self, *args):
        self.ids.timeri.text = str(int(self.ids.timeri.text) - 1)

额外优化

为了避免重复点击start按钮导致多个时钟调度同时运行,出现倒计时速度异常的问题,可以在启动调度前先判断已有调度是否存在,存在则先取消:

def timer_sec(self, *args):
    # 清理已有调度,避免重复运行
    if hasattr(self, 'function_interval') and self.function_interval.is_triggered:
        self.function_interval.cancel()
    self.function_interval = Clock.schedule_interval(self.update_label, 1)
    Clock.schedule_once(self.stop_interval, 59.5)

现有代码笔误修正

你当前代码中存在一处不匹配问题:MyApp.build方法中注册的screen名称是Begin1Afterdup,但start_off方法中跳转的目标screen名称是Beginning1after,同时调用的myapp.begin1after属性没有提前绑定,需要调整对应命名和属性绑定,否则会触发运行报错。

内容的提问来源于stack exchange,提问作者user16240688

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.12 19:54:03