Skip to content
Related Articles

Related Articles

Python | Create a stopwatch using clock object in kivy using .kv file

Improve Article
Save Article
  • Difficulty Level : Hard
  • Last Updated : 01 Feb, 2021
Improve Article
Save Article

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.

 Kivy Tutorial – Learn Kivy with Examples.

Clock Object: 
The Clock object allows you to schedule a function call in the future; once or repeatedly at specified intervals. 
You can get the time elapsed between the scheduling and the calling of the callback via the dt argument:


# define callback
def my_callback(dt):
# clock.schedule_interval with time specified
Clock.schedule_interval(my_callback, 0.5)
# clock.schedule_once with time specified
Clock.schedule_once(my_callback, 5)
# call my_callback as soon as possible.

Note: If the callback returns False, the schedule will be canceled and won’t repeat. 

In this, we are going to create the kivy the stopwatch and we are creating 3 buttons in this which are the start, pause, resume.

It is good to use kivy inbuilt module while working with clock and: 
from kivy.clock import Clock


Basic Approach:  
1) import kivy
2) import kivyApp
3) import Builder
4) import Boxlayout
5) Import clock
6) import kivy properties(only needed one)
7) Set minimum version(optional)
8) Create the .kv code:
     1) Create Buttons
     2) Add call to button
     3) Add label 
9) Create Layout class
10) Create App class
11) return Layout/widget/Class(according to requirement)
12) Run an instance of the class

# Implementation of the Approach: 


Code of How to create Stopwatch
# 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 import App
# this restrict the kivy version i.e 
# below this kivy version you cannot 
# use the app or software 
# The Builder is responsible for creating
# a Parser for parsing a kv file
from kivy.lang import Builder
# The Properties classes are used
# when you create an EventDispatcher.
from import NumericProperty
# BoxLayout arranges children in a vertical or horizontal box.
# or help to put the children at the desired location.
from kivy.uix.boxlayout import BoxLayout
# he Clock object allows you to
# schedule a function call in the future
from kivy.clock import Clock
# Create the .kv file and load it by using Builder
    # Assigning the alignment to buttons
        orientation: 'vertical'
        # Create Button
            text: 'start'
            on_press: root.start()
            text: 'stop'
            on_press: root.stop()
            text: 'Reset'
            on_press: root.number = 0
    # Create the Label
        text: str(round(root.number))
        text_size: self.size
        halign: 'center'
        valign: 'middle'
# Create the Layout class
class MainWidget(BoxLayout):
    number = NumericProperty()
    def __init__(self, **kwargs):
        # The super() builtin
        # returns a proxy object that
        # allows you to refer parent class by 'super'.
        super(MainWidget, self).__init__(**kwargs)
        # Create the clock and increment the time by .1 ie 1 second.
        Clock.schedule_interval(self.increment_time, .1)
    # To increase the time / count
    def increment_time(self, interval):
        self.number += .1
    # To start the count
    def start(self):
        Clock.schedule_interval(self.increment_time, .1)
    # To stop the count / time
    def stop(self):
# Create the App class
class TimeApp(App):
    def build(self):
        return MainWidget()
# Run the App



In this when you press start count start, when press Restart it starts again and when pause it get paused.

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!