Change language

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

Shop

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

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

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

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

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

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers


Wiki

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

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

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

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