Python | Create a stopwatch Using Clock Object in kivy

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 Desktops applications.

In this we are going to see how can we create a stopwatch using label .
In the code i am creating just a counter using label in which when you set the time in seconds it will start decreasing like a countdown and in the second we will do the same by using clock object.

Clock Object:
1) Kivy provides Clock objects.
2) Clock objects can be made to call a function when a specified time period has elapsed.
3) A clock object in Kivy can be configured to call a function upon every elapse of a time duration 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

'''
Code of How to create countdown using label only
'''
    
# 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 kivy.app import App 
      
# this restrict the kivy version i.e  
# below this kivy version you cannot  
# use the app or software  
kivy.require('1.9.0')
  
# The Label widget is for rendering text.
from kivy.uix.label import Label
  
# Animation is used to animate Widget properties
from kivy.animation import Animation
  
# The Properties classes are used when you create an EventDispatcher.
from kivy.properties import StringProperty, NumericProperty
  
  
# create a label class
class Clock(Label):
  
    # Set the numeric property
    # i.e set the counter number you can change it accoedingly
    a = NumericProperty(100# seconds
  
    # To start countdown
    def start(self):
        Animation.cancel_all(self# stop any current animations
        self.anim = Animation(a = 0, duration = self.a)
  
        # TO finish count down
        def finish_callback(animation, clock):
            clock.text = "FINISHED"
  
        self.anim.bind(on_complete = finish_callback)
        self.anim.start(self)
  
    # If u romove this theere will be nothing on screen
    def on_a(self, instance, value):
        self.text = str(round(value, 1))
  
          
# Create the App class
class TimeApp(App):
    def build(self):
        # Create the object of Clock class
        clock = Clock()
  
        # call the function from class Clock
        clock.start()
        return clock
  
# Run the App
if __name__ == "__main__":
    TimeApp().run()

chevron_right


Output:

Note: Countdown start from 100 and ends on 0

# Now By using Clock Object:

filter_none

edit
close

play_arrow

link
brightness_4
code

'''
Code of How to create countdown using label only
'''
    
# 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 kivy.app import App 
      
# this restrict the kivy version i.e  
# below this kivy version you cannot  
# use the app or software  
kivy.require('1.9.0')
  
# The Label widget is for rendering text.
from kivy.uix.label import Label
  
# The Clock object allows you to schedule
# a function call in the future; once or
# repeatedly at specified intervals.
from kivy.clock import Clock
   
# The kivy App that extends from the App class
class ClockDemo(App):
  
    count = 0
  
    def build(self):
       self.myLabel = Label(text ='Waiting for updates...')
  
       # Start the 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 app
if __name__ == '__main__':
    ClockDemo().run()

chevron_right


Output:

Note: This starts from 0 and run until you cut the window



My Personal Notes arrow_drop_up

Competative Programmer

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.