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):

  pass

 
# 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 kivy.app 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 kivy.properties 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 ( "" "

 
& lt; MainWidget & gt ;:

 

# Assign button alignment

  BoxLayout:

orientation: "vertical"

 

# Create button

  

  Button:

text: "start"

on_press: root.start ()

 

  Button:

  text: "stop"

on_press: root .stop ()

 

Button:

text: "Reset"

on_press: root.number = 0

  

  # Create shortcut

Tag:

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 ()

Output:

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