Python | Create a stopwatch Using Clock Object in kivy - GeeksforGeeks

29 Sep, 2021
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 Desktop applications.In this, we are going to see how can we create a stopwatch using a label. In the code, we will be creating just a counter using the 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. 
 Kivy Tutorial – Learn Kivy with Examples.
Clock Object: 
Kivy provides Clock objects.Clock objects can be made to call a function when a specified time period has elapsed.A clock object in Kivy can be configured to call a function upon every elapse of time duration or only once.
Kivy provides Clock objects.
Clock objects can be made to call a function when a specified time period has elapsed.
A clock object in Kivy can be configured to call a function upon every elapse of 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:
Python3
'''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 propertiesfrom kivy.animation import Animation # The Properties classes are used when you create an EventDispatcher.from kivy.properties import StringProperty, NumericProperty  # create a label classclass Clock(Label):     # Set the numeric property    # i.e set the counter number you can change it accordingly    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 remove this there will be nothing on screen    def on_a(self, instance, value):        self.text = str(round(value, 1))          # Create the App classclass 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 Appif __name__ == "__main__":    TimeApp().run()
Output:
Note: Countdown starts from 100 and ends on 0
# Now By using Clock Object:
Python3
'''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 classclass 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 appif __name__ == '__main__':    ClockDemo().run()
Output:
Note: This starts from 0 and runs until you cut the window  
abhigoya
simranarora5sos
Python-gui
Python-kivy
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Python Dictionary
Enumerate() in Python
How to Install PIP on Windows ?
Different ways to create Pandas Dataframe
Python String | replace()
Create a Pandas DataFrame from Lists
Selecting rows in pandas DataFrame based on conditions
Reading and Writing to text files in Python
sum() function in Python
*args and **kwargs in Python