Kivy: Check if a function is already scheduled

北城余情 提交于 2020-01-16 07:03:10

问题


Is there a way to check if a function has been scheduled already? I was looking at the Clock methods but didn't see anything I could use. I am trying to avoid a rescheduling(Clock.unschedule(func) -> Clock.schedule_interval(func, dt)) of a function unless it is already scheduled to begin with.


回答1:


You can use kivy.clock.Clock.get_events() to get the list of scheduled events:

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

from kivy.lang import Builder

Builder.load_string("""
#:import Clock kivy.clock.Clock

<MyWidget>:
    Button:
        text: "Print events"
        on_press: print(Clock.get_events())
    Button:
        text: "Add event"
        on_press: Clock.schedule_once(root.my_callback, 5)
""")

class MyWidget(BoxLayout):
    def my_callback(self, arg):
        print("my_callback")

class MyApp(App):
    def build(self):
        return MyWidget()

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

OLD ANSWER:

Not a very clean way, but you can examine the content of kivy.clock.Clock._events list.



来源:https://stackoverflow.com/questions/31254796/kivy-check-if-a-function-is-already-scheduled

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!