如何使用可中断的基维时钟?
How to use the intteruptible kivy clock?
我正在尝试在 kivy 中构建一个简单的秒表,我想使用 interruptible clock to properly create an interval that tracks 1/100th of a second (The normal clock isn't accurate enough it seems). But I can't really figure out how to properly integrate the interruptible clock。
我试着阅读它并尝试了这个
class TimerApp(App):
def build_config(self, config):
config.setdefaults('section1', {
'KIVY_CLOCK': 'interrupt'
})
def build(self):
config = self.config
return AppLayout()
然而,这似乎根本没有改变 Clock.schedule_interval 功能。解决此问题的正确方法是什么?我如何验证设置是否已更改?
以下示例尝试描述差异:
注意:我将此测试的超时设置为 1 秒(1000 毫秒)
使用默认设置:
from kivy.app import App
from kivy.clock import Clock
from kivy.uix.boxlayout import BoxLayout
import time
t = int(round(time.time() * 1000)) #current time in millisecond
def call_back(dt):
global t
t1 = int(round(time.time() * 1000))
print t1 - t
t = t1
clock = Clock.schedule_interval(call_back, 1)
class TimerApp(App):
def build(self):
return BoxLayout()
if __name__ == '__main__':
TimerApp().run()
输出是:
1002
1003
1006
1004
1006
1005
1004
1001
1003
1002
1003
如您所见,输出总是(几乎)> 1000 毫秒
使用中断配置:
...
from kivy.config import Config
...
class TimerApp(App):
def build(self):
Config.set('graphics', 'KIVY_CLOCK', 'interrupt')
Config.write()
return BoxLayout()
...
输出为:
997
998
1000
1000
998
998
1000
1001
1000
我正在尝试在 kivy 中构建一个简单的秒表,我想使用 interruptible clock to properly create an interval that tracks 1/100th of a second (The normal clock isn't accurate enough it seems). But I can't really figure out how to properly integrate the interruptible clock。
我试着阅读它并尝试了这个
class TimerApp(App):
def build_config(self, config):
config.setdefaults('section1', {
'KIVY_CLOCK': 'interrupt'
})
def build(self):
config = self.config
return AppLayout()
然而,这似乎根本没有改变 Clock.schedule_interval 功能。解决此问题的正确方法是什么?我如何验证设置是否已更改?
以下示例尝试描述差异:
注意:我将此测试的超时设置为 1 秒(1000 毫秒)
使用默认设置:
from kivy.app import App
from kivy.clock import Clock
from kivy.uix.boxlayout import BoxLayout
import time
t = int(round(time.time() * 1000)) #current time in millisecond
def call_back(dt):
global t
t1 = int(round(time.time() * 1000))
print t1 - t
t = t1
clock = Clock.schedule_interval(call_back, 1)
class TimerApp(App):
def build(self):
return BoxLayout()
if __name__ == '__main__':
TimerApp().run()
输出是:
1002
1003
1006
1004
1006
1005
1004
1001
1003
1002
1003
如您所见,输出总是(几乎)> 1000 毫秒
使用中断配置:
...
from kivy.config import Config
...
class TimerApp(App):
def build(self):
Config.set('graphics', 'KIVY_CLOCK', 'interrupt')
Config.write()
return BoxLayout()
...
输出为:
997
998
1000
1000
998
998
1000
1001
1000