Change language

Python | Create stopwatch using clock object in kiw using .kv file

| | |

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

# define callback

def my_callback (dt):


# clock.schedule_interval with the specified time

Clock.schedule_interval (my_callback, 0.5 )

# clock.schedule_once with the specified time

Clock.schedule_once (my_callback, 5 )

# call my_callback as soon as possible.
Clock.schedule_once (my_callback)

Note. If the callback returns False, the schedule will be canceled and will not repeat.

In this we are going to create a kivy stopwatch and we will create 3 buttons to 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:

" ""
as with create stopwatch
"" "

# Program to show how to create a radio button
# import nodded module

import kivy 

# Your application base class inherits from the application class.
# app: always refers to your application instance

from import App 

# this limits the kivy version ie
# below this version you cannot
# use the application or software

kivy.require ( ’1.9.0’ )

# Builder is responsible for creating
# parser to parse the kv file

from kivy.lang import Builder

# Property classes are used
# when you create an EventDispatcher.

from import NumericProperty

# BoxLayout places children in a vertical or horizontal border.
# or help put children in desired location.

from kivy.uix.boxlayout import BoxLayout 

# The Clock object allows you
# to schedule a function call in the future

from kivy.clock import Clock


# Create a file. kv and load it using Builder

Builder.load_string ( "" "

"MainWidget & gt ;:


# Assign button alignment


orientation: "vertical"


# Create button



text: "start"

on_press: root.start ()



  text: "stop"

on_press: root .stop ()



text: "Reset"

on_press: root.number = 0


  # Create shortcut


text: str (round (root.number))

text_size: self.size

halign: & # 39; center & # 39;

valign: "medium"

"" " )

# Create Layout class

class MainWidget (BoxLayout):


  number = NumericProperty ()


def __ init __ ( self , * * kwargs):


# Super () inline

# returns a proxy that

< p> # allows you to reference the parent class as & # 39; super & # 39 ;.

super (MainWidget, self ) .__ init __ ( * * kwargs)


  # Create a clock and increase the time by 0.1, which is 1 second.

Clock.schedule_interval ( self . increment_time,. 1 )


self . increment_t ime ( 0 )


# To increase the time / amount

def increment_time ( self , interval):

self . number + = . 1


  # To start counting

def start ( self ):


  Clock.unschedule ( self . increment_time)

Clock .schedule_interval ( self . increment_time,. 1 )


# To stop counting / time

def stop ( self ):

Clock.unschedule ( self .increment_time)

# Create a class pr Attachments

class TimeApp (App):

def build ( self ):

return MainWidget ()

# Run the application
TimeApp (). run ()


However, when you click Start start start, when you click Restart, it starts again, and when paused, it stops.


Learn programming in R: courses


Best Python online courses for 2022


Best laptop for Fortnite


Best laptop for Excel


Best laptop for Solidworks


Best laptop for Roblox


Best computer for crypto mining


Best laptop for Sims 4


Latest questions


Common xlabel/ylabel for matplotlib subplots

12 answers


How to specify multiple return types using type-hints

12 answers


Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers


Flake8: Ignore specific warning for entire file

12 answers


glob exclude pattern

12 answers


How to avoid HTTP error 429 (Too Many Requests) python

12 answers


Python CSV error: line contains NULL byte

12 answers


csv.Error: iterator should return strings, not bytes

12 answers



Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python


How to specify multiple return types using type-hints


Printing words vertically in Python


Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries


Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically