Totem
Totem

Reputation: 7369

Kivy: Check if a function is already scheduled

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.

Upvotes: 1

Views: 784

Answers (1)

Nykakin
Nykakin

Reputation: 8747

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.

Upvotes: 2

Related Questions