Clock objects in Kivy

Clock objects in Kivy can be configured to make calls to a callback function one or more times. An example app is given along with the screenshot.

Kivy Updated 4 May 2017 1 min read

Overview

  • Kivy provides Clock objects.

  • Clock objects can be made to call a function when a specified time period has elapsed.

  • For Example, a sound alert can be played when a time period elapses.

  • A clock object in Kivy can be configured to call a function upon every elapse of a time duration or only once.

Example

python
from kivy.app import App

from kivy.clock import Clock

from kivy.uix.label import Label

# The kivy App that extends from the App class

class ClockDemo(App):

    count = 0

    def build(self):

       self.myLabel = Label(text='Waiting for updates...')

       #Start the clock

       Clock.schedule_interval(self.Callback_Clock, 2)

       return self.myLabel

    def Callback_Clock(self, dt):

        self.count = self.count+1

        self.myLabel.text = "Updated %d...times"%self.count

# Run the app

if __name__ == '__main__':

    ClockDemo().run()

Output

Example for using clock objects in a kivy app