调用ON_PAUSE时暂停计时器似乎不起作用(Kivy)

原学程将引见挪用ON_PAUSE时暂停计时器仿佛没有起感化(Kivy)的处置办法,这篇学程是从其余处所瞅到的,而后减了1些海外法式员的疑问与解问,愿望能对于您有所赞助,佳了,上面开端进修吧。

调用ON_PAUSE时暂停计时器似乎不起作用(Kivy) 教程 第1张

成绩描写

在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, 一)

  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 = 一.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)的学程便到这里便停止了,愿望趣模板源码网找到的这篇技巧文章能赞助到年夜野,更多技巧学程不妨在站内搜刮。