Python | Create a stopwatch Using Clock Object in kivy (original) (raw)
Last Updated : 12 Jul, 2025
Kivy is a platform-independent GUI tool in Python. As it can be run on Android, IOS, Linux and Windows, etc. It is basically used to develop the Android application, but it does not mean that it can not be used on Desktop applications.
In this, we are going to see how can we create a stopwatch using a label.
In the code, we will be creating just a counter using the label in which when you set the time in seconds it will start decreasing like a countdown and in the second we will do the same by using clock object.
Clock Object:
- Kivy provides Clock objects.
- Clock objects can be made to call a function when a specified time period has elapsed.
- A clock object in Kivy can be configured to call a function upon every elapse of time duration or only once.
It is good to use kivy inbuilt module while working with clock:
from kivy.clock import Clock
Basic Approach:
- import kivy
- import kivyApp
- import label
- import Animation
- Import clock
- import kivy properties(only needed one)
- Set minimum version(optional)
- Create Label class
- Create App class
- return Layout/widget/Class(according to requirement)
- Run an instance of the class
# Simple Approach:
Python3 `
''' Code of How to create countdown using label only '''
Program to Show how to create a switch
import kivy module
import kivy
base Class of your App inherits from the App class.
app:always refers to the instance of your application
from kivy.app import App
this restrict the kivy version i.e
below this kivy version you cannot
use the app or software
kivy.require('1.9.0')
The Label widget is for rendering text.
from kivy.uix.label import Label
Animation is used to animate Widget properties
from kivy.animation import Animation
The Properties classes are used when you create an EventDispatcher.
from kivy.properties import StringProperty, NumericProperty
create a label class
class Clock(Label):
# Set the numeric property
# i.e set the counter number you can change it accordingly
a = NumericProperty(100) # seconds
# To start countdown
def start(self):
Animation.cancel_all(self) # stop any current animations
self.anim = Animation(a = 0, duration = self.a)
# TO finish count down
def finish_callback(animation, clock):
clock.text = "FINISHED"
self.anim.bind(on_complete = finish_callback)
self.anim.start(self)
# If u remove this there will be nothing on screen
def on_a(self, instance, value):
self.text = str(round(value, 1))
Create the App class
class TimeApp(App): def build(self): # Create the object of Clock class clock = Clock()
# call the function from class Clock
clock.start()
return clockRun the App
if name == "main": TimeApp().run()
`
Output:
Note: Countdown starts from 100 and ends on 0
# Now By using Clock Object:
Python3 `
''' Code of How to create countdown using label only '''
Program to Show how to create a switch
import kivy module
import kivy
base Class of your App inherits from the App class.
app:always refers to the instance of your application
from kivy.app import App
this restrict the kivy version i.e
below this kivy version you cannot
use the app or software
kivy.require('1.9.0')
The Label widget is for rendering text.
from kivy.uix.label import Label
The Clock object allows you to schedule
a function call in the future; once or
repeatedly at specified intervals.
from kivy.clock import Clock
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, 1)
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:
Note: This starts from 0 and runs until you cut the window