Stopwatch in kivy using Clock object
This post is about the development of stopwatch in kivy using the clock object of kivy.
>>> Basically, clock object is about scheduling of intervals after specific time (time should be in seconds) and we can schedule it for once only and we can unschedule it whenever we want.
Here is the code with detailed comments 👇👇👇👇👇:-
from kivy.base import runTouchApp
from kivy.lang import Builder
from kivy.clock import Clock
from kivy.config import Config
from kivy.uix.screenmanager import Screen, ScreenManager
#setting up the size of window
Config.set('graphics', 'resizable', True)
Config.set('graphics', 'width', 350)
Config.set('graphics', 'height', 300)
# global variables for time
msec = 0
mmin = 0
mhr = 0
class Stopwatch(Screen):
def start_func(self, tim):
global event
# event function is to check the seconds and convert it into minutes and then minutes into hours and increase seconds by 1 on everytime it is called.
def event(self):
global msec, mmin, mhr
msec += 1
if msec == 60:
mmin += 1
msec = 0
if mmin == 60:
mhr += 1
mmin = 0
tim.text = f'{str(mhr)} : {str(mmin)} : {str(msec)}'
global msec, mmin, mhr
# here we schedule our event function after 1 second. its means event function executes after every second until we unschedule it
Clock.schedule_interval(event, 1)
# when user presses start button then stopwatch starts and start button will be disabled while stop and reset buttons remain enabled
self.ids.strt_btn.disabled = True
self.ids.stop_btn.disabled = False
self.ids.rst_btn.disabled = False
def stop_func(self):
# when user presses stop button it will unschedule event function and stopwatch stops.
Clock.unschedule(event)
self.ids.strt_btn.disabled = False
# and here text of start button changes to continue and stop button will be disabled.
self.ids.strt_btn.text = "Continue"
self.ids.stop_btn.disabled = True
self.ids.rst_btn.disabled = False
def reset_func(self):
global mmin, msec, mhr
# when user presses reset button it will unschedule event function if it is scheduled and start button's text changes to start again
Clock.unschedule(event)
self.ids.strt_btn.disabled = False
self.ids.strt_btn.text = "Start"
self.ids.stop_btn.disabled = True
self.ids.rst_btn.disabled = True
self.ids.tim.text = 'Hr : Min : Sec'
msec = mhr = mmin = 0
class Manager(ScreenManager):
pass
# in kv file I have designed GUI for this program
runTouchApp(Builder.load_string('''
Manager:
Stopwatch:
<Stopwatch>:
FloatLayout:
canvas.before:
Color:
rgba: 0,0,0,1
Rectangle:
size: self.size
pos: self.pos
Label:
text: 'Hr : Min : Sec'
size_hint: (.1, .1)
pos_hint: {'x': .45, 'y': .7}
font_size: 35
color: 1,1,1,1
id: tim
Button:
text: 'start'
id: strt_btn
size_hint: (.25, .15)
pos_hint: {'x': .05, 'y': .25}
background_color: [0,1,.05,1]
on_release:
root.start_func(tim)
Button:
text: 'stop'
id: stop_btn
size_hint: (.25, .15)
pos_hint: {'x': .38, 'y': .25}
background_color: [1,0,0,1]
on_release:
root.stop_func()
Button:
text: 'Reset'
id: rst_btn
size_hint: (.25, .15)
pos_hint: {'x': .7, 'y': .25}
background_color: [1,1,0,1]
on_release:
root.reset_func()
'''))
Comments