【问题标题】:How to use the intteruptible kivy clock?如何使用 intteruptible kivy 时钟?
【发布时间】:2018-05-10 00:03:49
【问题描述】:

我正在尝试在 kivy 中构建一个简单的秒表,我想使用 interruptible clock 正确创建一个跟踪 1/100 秒的间隔(正常时钟似乎不够准确) .但我真的不知道如何正确集成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 函数。解决此问题的正确方法是什么?如何验证设置是否已更改?

【问题讨论】:

    标签: python kivy kivy-language


    【解决方案1】:

    以下示例试图描述差异:

    注意:我将此测试的超时设置为 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
    

    【讨论】:

      猜你喜欢
      • 2013-09-26
      • 2021-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多