Python | Create a stopwatch using Clock Object in kivy



In this, we`ll see how we can create a stopwatch using a label. 
In the code, I only create a counter using a label where when you set the time in seconds it will start decreasing like a countdown, and in the second we will do the same using a clock object.

Clock Object:
1) Kiwi provides clock to objects. 
2) Clock objects can be made to call a function after a specified period of time. 
3) The clock object in Kivy can be configured to call a function after a certain amount of time, or only once.

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

  Basic Approach:  1) import kivy 2) import kivyApp 3) import label 4) import Animation 5)  Import clock  6) import kivy properties (only needed one) 7) Set minimum version (optional) 8) Create Label class 9) Create App class 10) return Layout / widget / Class ( according to requirement) 11) Run an instance of the class 

# Simple approach:

"" »
Code How to create a countdown using only the tag
" ""

 
# Program to show how to create a radio button
# import nodding 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` )

  
# The Label widget is used to render text.

from kivy.uix.label import Label

 
# Animation is used to animate widget properties

from kivy.animation import Animation

 
# Property classes are used when creating the EventDispatcher.

from kivy.properties import StringProperty, NumericProperty

 

  
# create label class

class Clock (Label):

 

# Set numeric property

# i.e. set counter number, you can change it accordingly

a = NumericProperty ( 100 # seconds

 

# To start countdown

def start ( self ):

Animation.cancel_all ( self # stop any current animation

self . anim = Animation (a = 0 , duration = self . a)

 

# To complete the reverse countdown

def finish_callback (animation, clock):

clock.text = "FINISHED"

 

self . anim.bind (on_complete = finish_callback)

self . Anim.start ( self )

 

# If you do this, nothing appears on the screen

def on_a ( self , instance, value):

self . text = str ( round ( value, 1 ))

 

 
# Create application class

class  TimeApp (App):

def build ( self ):

# Create an object of the Clock class

clock = Clock ()

 

# call a function from the Clock class

clock.start ()

  return clock

  
# Run the application

if __ name __ = = "__ main__" :

TimeApp (). run ()

Exit:

Note: Countdown starts at 100 and ends at 0

# Now with Clock Object:

Exit:

Note: this starts at 0 and continues until you cut the window


“ ””
Code How to create a countdown using just the label
"" "

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

< code class = "keyword"> 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` )

 
# The Label widget is used to render text.

from kivy.uix.label import Label

 
# The Clock object allows scheduling
# function call in the future; once or
# multiple times at specified intervals.

from kivy.clock import Clock

 
# A live application that extends from the App class

class ClockDemo (App):

 

count = 0

 

def build ( self ):

self . myLabel = Label (text = ` Waiting for updates .. .` )

 

# Start 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 application

if __ name__ = = `__main__ ` :

  ClockDemo (). run ()