Skip to content

Instantly share code, notes, and snippets.

@casperlehmann
Last active June 30, 2022 13:52
Show Gist options
  • Save casperlehmann/740185c161c386ddaf2a to your computer and use it in GitHub Desktop.
Save casperlehmann/740185c161c386ddaf2a to your computer and use it in GitHub Desktop.
Stop watch implemented with kivy.clock.Clock
from kivy.app import App
from kivy.lang import Builder
from kivy.properties import NumericProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.clock import Clock
Builder.load_string('''
<MainWidget>:
BoxLayout:
orientation: 'vertical'
Button:
text: 'start'
on_press: root.start()
# Choose an image and uncomment:
#background_down: 'button_pressed.png'
#background_normal: 'button_normal.png'
#color: [0, 0, 0, 1]
Button:
text: 'stop'
on_press: root.stop()
# If you have a favourite font, you can use that:
#font_name: 'font_name.ttf'
#font_size: '22dp'
Button:
text: 'Set to 0'
on_press: root.number = 0
Label:
text: str(root.number)
text_size: self.size
halign: 'center'
valign: 'middle'
''')
class MainWidget(BoxLayout):
number = NumericProperty()
def __init__(self, **kwargs):
super(MainWidget, self).__init__(**kwargs)
Clock.schedule_interval(self.increment_time, .1)
self.increment_time(0)
def increment_time(self, interval):
self.number += .1
def start(self):
Clock.unschedule(self.increment_time)
Clock.schedule_interval(self.increment_time, .1)
def stop(self):
Clock.unschedule(self.increment_time)
class ExampleApp(App):
def build(self):
return MainWidget()
ExampleApp().run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment