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 Kivy, App 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!