调用ON_PAUSE时暂停计时器似乎不起作用(Kivy) [英] Pausing timer when on_pause is called doesnt seem to work (Kivy)

查看:0
本文介绍了调用ON_PAUSE时暂停计时器似乎不起作用(Kivy)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android上,我正在尝试在调用on_pause时暂停我的UPDATE_TIME方法,并在 On_Resume被调用。 换句话说,我试图在应用程序最小化时暂停计时器,并在应用程序被最小化时恢复计时器 已经恢复了。 我知道时钟一直在后台运行,但我搞不懂 为什么代码不起作用。如果self.sw_started==True";被忽略,或者";self.sw_started";永远不会变为False,则检查看起来就像是检查。 例:我打开应用程序,它开始计时。然后我把它最小化,等了几秒钟。 当我恢复它时,它不再从原来的位置继续,这是我正在寻找的行为。

非常非常感谢您的宝贵时间,并对我糟糕的英语表示歉意!

from kivy.app import App
from kivy.uix.label import Label
from kivy.clock import Clock


class MainApp(App):
    def build(self):
        self.sw_started = False
        self.sw_seconds = 0
        #self.tmp_sw_seconds = 0 #this way doesnt work either
        Clock.schedule_interval(self.update_time, 1)

        self.lbl=Label(text="0")
        return self.lbl

    def on_start(self):
        self.start_timer()

    def update_time(self, nap):
        if self.sw_started==True:
            self.sw_seconds += nap
            print(round(self.sw_seconds))
            self.lbl.text=str(round(self.sw_seconds))

    def reset_timer(self):
        self.sw_started = False
        self.sw_seconds = 0

    def start_timer(self):
        self.sw_started = True

    def stop_timer(self):
        self.sw_started=False

    def on_pause(self):
        self.stop_timer()
        # self.tmp_sw_seconds = self.sw_seconds #this way doesnt work either
        print("timer paused")
        return True

    def on_resume(self):
        # self.sw_seconds = self.tmp_sw_seconds #this way doesnt work either
        self.start_timer()
        print("timer resumed")

        pass
MainApp().run()

推荐答案

您可以将sw_started更改为sw_started = BooleanProperty(False)。这将使您能够在其值(此处为bool)更改时观察其当前状态。

接下来,您可以将其绑定到任何所需的回调方法,或者只需使用on_属性方法。

因此,更新后的代码现在应该如下所示

from kivy.app import App
from kivy.uix.label import Label
from kivy.clock import Clock
from kivy.properties import BooleanProperty


class MainApp(App):
    sw_started = BooleanProperty(False)
    
    def on_sw_started(self, *args):
        # This will call meth. update_time whenever attr. sw_started changes.
        self.update_time(args)
    
    def build(self):
        self.frequency = 1.0
#        self.bind(sw_started = self.update_time)
        self.sw_seconds = 0
        #self.tmp_sw_seconds = 0 #this way doesnt work either
        Clock.schedule_interval(self.update_time, self.frequency)

        self.lbl=Label(text="0")
        return self.lbl

    def on_start(self):
        self.sw_started = True
#        self.start_timer()

    def update_time(self, *args):
        if self.sw_started:
#        if self.sw_started==True:
            self.sw_seconds += self.frequency
            print(int(self.sw_seconds))
            self.lbl.text=str(int(self.sw_seconds))

    def reset_timer(self):
        self.sw_started = False
        self.sw_seconds = 0

#    def start_timer(self):
#        self.sw_started = True

#    def stop_timer(self):
#        self.sw_started=False

    def on_pause(self):
        self.sw_started = False
#        self.stop_timer()
        # self.tmp_sw_seconds = self.sw_seconds #this way doesnt work either
        print("timer paused")
        return True

    def on_resume(self):
        # self.sw_seconds = self.tmp_sw_seconds #this way doesnt work either
#        self.start_timer()
        print("timer resumed")
        self.sw_started = True

#        pass
MainApp().run()

这篇关于调用ON_PAUSE时暂停计时器似乎不起作用(Kivy)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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