Table of contents
1.
Introduction
2.
Clock Object
3.
Countdown Clock Implementation
3.1.
Output
4.
Stopwatch Implementation
4.1.
Output
5.
Frequently Asked Questions
5.1.
What is a widget in Python Kivy?
5.2.
What is object property in Kivy?
5.3.
How to install Kivy on Windows?
5.4.
 
5.5.
What is Kivy Clock?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Using Clock Objects in Kivy

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Application development, Desktop, and Android are a significant part of python programming. That is where Kivy can be a handy tool for python developers. Kivy is a platform-independent GUI tool that you can run on Android, IOS, Linux, Windows, or any other operating system. Although we majorly utilize Kivy for Android development, its functionality is not limited.

You can find a lot of exciting functionalities and implementations of Kivy in our articles like Float Layout in KivyCarousal Widget in KivyButton Action in Kivy, and Slider Widget in Kivy.

In this article, we will explore the implementation of the Clock Object in Kivy python to create a Stopwatch with the help of examples. This article assumes that you have worked with Python programming. If that is not the case, you can check out our Basics of Python article and come back here.

So let the countdown begin!

 

 

Clock Object

The Clock object allows us to schedule a function call in the future, once or repeatedly at given intervals. You can configure the clock object to call a function upon every time interval elapsed or only once where specified.

You can import the Clock object from the inbuilt module available in Kivy.

from kivy.clock import Clock
You can also try this code with Online Python Compiler
Run Code

Below is a straightforward example of using the clock object for these core implementations.

# dt means delta-time
def myfirstcallback(dt):
    pass


# call myfirstcallback every second
Clock.schedule_interval(my_callback, 1)


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


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

You can also try this code with Online Python Compiler
Run Code

You can learn more about the clock object by going through the official documentation here. In this article, we will use the Clock object to create a Stopwatch and a countdown clock with the help of a label.

Countdown Clock Implementation

Our first code will create just a counter using the label class in which when you set the time in seconds, it will start decreasing like a countdown clock. 

We will start by importing all the necessary modules like core KivyApp class that refers to the instance of our application, Label widget for rendering text, Animation to animate widget properties, and some property classes.

import kivy
       
from kivy.app import App
     
kivy.require('1.9.0')
 
from kivy.uix.label import Label
 
from kivy.animation import Animation
 
from kivy.properties import StringProperty, NumericProperty
You can also try this code with Online Python Compiler
Run Code

Now we will create our label class. Here, we will set clock properties like the counter number that you can configure as the NumericProperty. Define functions to start the countdown using Animation, and end the countdown with a display text. 

class Clock(Label):
 
    a = NumericProperty(100)  # seconds
 
    def start(self):
        Animation.cancel_all(self)  # stop current animations
        self.anim = Animation(a = 0, duration = self.a)
 
        def finish_callback(animation, clock):
            clock.text = "FINISHED"
 
        self.anim.bind(on_complete = finish_callback)
        self.anim.start(self)
 
    def on_a(self, instance, value):
        self.text = str(round(value, 1))
You can also try this code with Online Python Compiler
Run Code

 Finally, we can create our App class that will create the object of the Clock class and call the start function from that class.

class TimeApp(App):
    def build(self):
       
        clock = Clock()
 
       
        clock.start()
        return clock
 
# Run the App
if __name__ == "__main__":
    TimeApp().run()
 
You can also try this code with Online Python Compiler
Run Code

Output

Stopwatch Implementation

Now we will use the Clock Object to create a Stopwatch. You will need to import the clock from kivy and App and Label.

       

from kivy.app import App
     
kivy.require('1.9.0')
 
from kivy.uix.label import Label
 
from kivy.clock import Clock
You can also try this code with Online Python Compiler
Run Code

Now we'll create our ClockDemo class. Here we will start the clock and schedule the message to display after every second on the clock.

class ClockDemo(App):
 
    count = 0
 
    def build(self):
       self.myLabel = Label(text ='Waiting for Ninja Coders...')
 
       # Start the clock
       Clock.schedule_interval(self.Callback_Clock, 1)
       
       return self.myLabel
 
    def Callback_Clock(self, dt):
        self.count = self.count + 1
        self.myLabel.text = "Ninja Coder Number % d"% self.count
 
       
# Run the app
if __name__ == '__main__':
    ClockDemo().run()
You can also try this code with Online Python Compiler
Run Code

Output

Frequently Asked Questions

What is a widget in Python Kivy?

A Widget is the building block of GUI interfaces in Kivy, providing a Canvas that we can use to draw on the screen. It receives events and reacts to them.

Kivy organizes widgets in trees. The application has a root widget, which usually has children who can have their children. Children of a widget are represented as the children attribute, a Kivy ListProperty.

 

What is object property in Kivy?

ObjectProperty is a specialized sub-class of the Property class in Kivy. It has the same initialization parameters. A Property always takes a default value and None in a particular case.

 

How to install Kivy on Windows?

The simplest way to install Kivy in Windows is by using pip. 

python -m pip install kivy

Optionally, you can install Kivy examples with Kivy.

python -m pip install kivy_examples

 

What is Kivy Clock?

The Clock object in Kivy allows you to schedule a function call in the future, once or repeatedly at given intervals. You can configure the clock object to call a function upon every time interval elapsed or only once where specified.

You can import the Clock object from the inbuilt module available in Kivy.

from kivy.clock import Clock
You can also try this code with Online Python Compiler
Run Code

Conclusion

In this article, we have extensively discussed the Clock Object in Kivy and its implementation to create a Stopwatch using Python with the help of examples. There are many more Python frameworks and tools that you can learn and utilize for application development.

We hope that this blog has helped you enhance your knowledge of Kivy Python and if you would like to learn more, check out our articles on Basics of Python and Popular Python Libraries. Head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, and much more.!

Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass