简体   繁体   English

如何使用 kivy 每分钟调用一次 function?

[英]How to call a function every minute using kivy?

when I press on a button i would like run a function everey minute.当我按下按钮时,我想每分钟运行一次 function。 I am using sched.scheduler but my kivy app crash.我正在使用sched.scheduler但我的 kivy 应用程序崩溃。

s.enter(60, 1, self.graph_data, (s,))
s.run()

Someone can help me?有人可以帮助我吗?

You can use the Clock object provided by kivy :您可以使用kivy 提供的时钟 object

import kivy

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.clock import Clock
    
class TestApp(App):
    
    def build(self):
        self.timer = None
        btn1 = Button(text='Click to start')
        btn1.bind(state=self.callbackStart)
        btn2 = Button(text='Click to stop')
        btn2.bind(state=self.callbackStop)

        bl = BoxLayout()
        bl.add_widget(btn1)
        bl.add_widget(btn2)
        return bl

    def callbackStart(self, instance, value):
        print('My button1 <%s> state is <%s>' % (instance, value))
        if self.timer is not None:
            Clock.unschedule(self.timer)
        self.timer = Clock.schedule_interval(self.timedAction, 0.5)        

    def callbackStop(self, instance, value):
        print('My button2 <%s> state is <%s>' % (instance, value))
        Clock.unschedule(self.timer)


    def timedAction(self, dt):
        print("Repeat")

if __name__ == '__main__':
    TestApp().run()

You should also be able to do something like this by using the threading build-in module with the Thread and Event objects.您还应该能够通过使用带有 Thread 和 Event 对象的 threading 内置模块来执行此类操作。

import kivy

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout

import threading


class MyTimedAction(threading.Thread):
    def __init__(self, event):
        threading.Thread.__init__(self)
        self.stopped = event

    def run(self):
        while not self.stopped.wait(0.5):
            print("Periodic action performed")


class TestApp(App):
    
    def build(self):
        # Create the stopTimer event. Initialize as stopped.
        self.stopTimer = threading.Event()
        self.stopTimer.set()

        btn1 = Button(text='Click to start')
        btn1.bind(state=self.callbackStart)
        btn2 = Button(text='Click to stop')
        btn2.bind(state=self.callbackStop)

        bl = BoxLayout()
        bl.add_widget(btn1)
        bl.add_widget(btn2)
        return bl

    def callbackStart(self, instance, value):
        print('My button1 <%s> state is <%s>' % (instance, value))
        if self.stopTimer.isSet():  # (Eventually) avoid duplicates if timer already started
            self.stopTimer.clear()
            thread = MyTimedAction(self.stopTimer)
            thread.start()

    def callbackStop(self, instance, value):
        print('My button2 <%s> state is <%s>' % (instance, value))
        self.stopTimer.set()

if __name__ == '__main__':
    TestApp().run()

The examples above assumes that if the timer is already started it doesn't start again another timed job on a new thread.上面的示例假设如果计时器已经启动,它不会在新线程上再次启动另一个定时作业。 The brief GUI code is just for demo purpose.简短的 GUI 代码仅用于演示目的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM