Quick search

Clock object

The Clock object allows you to schedule a function call in the future; once or repeatedly at specified intervals:

def my_callback(dt):
    pass

# call my_callback every 0.5 seconds
Clock.schedule_interval(my_callback, 0.5)

# call my_callback in 5 seconds
Clock.schedule_once(my_callback, 5)

# call my_callback as soon as possible (usually next frame.)
Clock.schedule_once(my_callback)

Note

If the callback returns False, the schedule will be removed.

If you want to schedule a function to call with default arguments, you can use the functools.partial python module:

from functools import partial

def my_callback(value, key, *largs):
    pass

Clock.schedule_interval(partial(my_callback, 'my value', 'my key'), 0.5)

Conversely, if you want to schedule a function that doesn’t accept the dt argument, you can use a lambda expression to write a short function that does accept dt. For Example:

def no_args_func():
    print("I accept no arguments, so don't schedule me in the clock")

Clock.schedule_once(lambda dt: no_args_func(), 0.5)

Note

You cannot unschedule an anonymous function unless you keep a reference to it. It’s better to add *args to your function definition so that it can be called with an arbitrary number of parameters.

Important

The callback is weak-referenced: you are responsible for keeping a reference to your original object/callback. If you don’t keep a reference, the ClockBase will never execute your callback. For example:

class Foo(object):
    def start(self):
        Clock.schedule_interval(self.callback, 0.5)

    def callback(self, dt):
        print('In callback')

# A Foo object is created and the method start is called.
# Because no reference is kept to the instance returned from Foo(),
# the object will be collected by the Python Garbage Collector and
# your callback will be never called.
Foo().start()

# So you should do the following and keep a reference to the instance
# of foo until you don't need it anymore!
foo = Foo()
foo.start()

Schedule before frame

New in version 1.0.5.

Sometimes you need to schedule a callback BEFORE the next frame. Starting from 1.0.5, you can use a timeout of -1:

Clock.schedule_once(my_callback, 0) # call after the next frame
Clock.schedule_once(my_callback, -1) # call before the next frame

The Clock will execute all the callbacks with a timeout of -1 before the next frame even if you add a new callback with -1 from a running callback. However, Clock has an iteration limit for these callbacks: it defaults to 10.

If you schedule a callback that schedules a callback that schedules a .. etc more than 10 times, it will leave the loop and send a warning to the console, then continue after the next frame. This is implemented to prevent bugs from hanging or crashing the application.

If you need to increase the limit, set the max_iteration property:

from kivy.clock import Clock
Clock.max_iteration = 20

Triggered Events

New in version 1.0.5.

A triggered event is a way to defer a callback exactly like schedule_once(), but with some added convenience. The callback will only be scheduled once per frame even if you call the trigger twice (or more). This is not the case with Clock.schedule_once():

# will run the callback twice before the next frame
Clock.schedule_once(my_callback)
Clock.schedule_once(my_callback)

# will run the callback once before the next frame
t = Clock.create_trigger(my_callback)
t()
t()

Before triggered events, you may have used this approach in a widget:

def trigger_callback(self, *largs):
    Clock.unschedule(self.callback)
    Clock.schedule_once(self.callback)

As soon as you call trigger_callback(), it will correctly schedule the callback once in the next frame. It is more convenient to create and bind to the triggered event than using Clock.schedule_once() in a function:

from kivy.clock import Clock
from kivy.uix.widget import Widget

class Sample(Widget):
    def __init__(self, **kwargs):
        self._trigger = Clock.create_trigger(self.cb)
        super(Sample, self).__init__(**kwargs)
        self.bind(x=self._trigger, y=self._trigger)

    def cb(self, *largs):
        pass

Even if x and y changes within one frame, the callback is only run once.

Note

ClockBase.create_trigger() also has a timeout parameter that behaves exactly like ClockBase.schedule_once().

kivy.clock.Clock = None

Instance of ClockBase.

class kivy.clock.ClockBase[source]

Bases: kivy.clock._ClockBase

A clock object with event support.

create_trigger(callback, timeout=0)[source]

Create a Trigger event. Check module documentation for more information.

New in version 1.0.5.

frames[source]

Number of internal frames (not necesseraly drawed) from the start of the clock.

New in version 1.8.0.

frames_displayed[source]

Number of displayed frames from the start of the clock.

frametime[source]

Time spent between the last frame and the current frame (in seconds).

New in version 1.8.0.

get_boottime()[source]

Get the time in seconds from the application start.

get_fps()[source]

Get the current average FPS calculated by the clock.

get_rfps()[source]

Get the current “real” FPS calculated by the clock. This counter reflects the real framerate displayed on the screen.

In contrast to get_fps(), this function returns a counter of the number of frames, not the average of frames per second.

get_time()[source]

Get the last tick made by the clock.

max_iteration

New in version 1.0.5: When a schedule_once is used with -1, you can add a limit on how iteration will be allowed. That is here to prevent too much relayout.

schedule_interval(callback, timeout)[source]

Schedule an event to be called every <timeout> seconds.

schedule_once(callback, timeout=0)[source]

Schedule an event in <timeout> seconds. If <timeout> is unspecified or 0, the callback will be called after the next frame is rendered.

Changed in version 1.0.5: If the timeout is -1, the callback will be called before the next frame (at tick_draw()).

tick()[source]

Advance the clock to the next step. Must be called every frame. The default clock has a tick() function called by the core Kivy framework.

tick_draw()[source]

Tick the drawing counter.

unschedule(callback)[source]

Remove a previously scheduled event.

kivy.clock.mainthread(func)[source]

Decorator that will schedule the call of the function in the mainthread. It can be useful when you use UrlRequest or when you do Thread programming: you cannot do any OpenGL-related work in a thread.

Please note that this method will return directly and no result can be returned:

@mainthread
def callback(self, *args):
    print('The request succedded!'
          'This callback is call in the main thread')

self.req = UrlRequest(url='http://...', on_success=callback)

New in version 1.8.0.