Skip to content

Instantly share code, notes, and snippets.

/stopwatch.py

Created May 30, 2017
Embed
What would you like to do?
stopwatch.py
import ui
from timerview import TimerView
class StopWatch(TimerView):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.current_start_time = 0
self.value = 0
self.state = 'stop'
def draw(self):
t0 = (self.value//(36000), self.value//600, self.value//10)
t1 = (t0[0], t0[1]%60, t0[2]%60)
ui.draw_string("{:02}:{:02}:{:02}".format(*t1),
font=('Helvetica', 20),
rect=(150, 0, 0, 0),
color='black',
alignment=ui.ALIGN_CENTER)
def update(self):
if self.state == 'run':
self.value = int((self.start_time - self.current_start_time)*10)
self.set_needs_display()
def button_action(sender):
v1 = sender.superview['view1']
if sender.title == 'Reset':
v1.value = 0
v1.state = 'stop'
elif sender.title == 'Start':
v1.value = 0
v1.current_start_time = v1.start_time
v1.state = 'run'
elif sender.title == 'Stop':
v1.state = 'stop'
v = ui.load_view()
v.present('sheet')
[
{
"class" : "View",
"attributes" : {
"background_color" : "RGBA(1.000000,1.000000,1.000000,1.000000)",
"tint_color" : "RGBA(0.000000,0.478000,1.000000,1.000000)",
"enabled" : true,
"border_color" : "RGBA(0.000000,0.000000,0.000000,1.000000)",
"flex" : ""
},
"frame" : "{{0, 0}, {397, 271}}",
"selected" : false,
"nodes" : [
{
"class" : "View",
"attributes" : {
"class" : "View",
"name" : "view1",
"uuid" : "80BC4BC3-1F0A-45A1-94AD-680AF1DA4FE2",
"frame" : "{{70, 70}, {100, 100}}",
"custom_class" : "StopWatch"
},
"frame" : "{{17, 27}, {345, 76}}",
"selected" : false,
"nodes" : [
]
},
{
"class" : "Button",
"attributes" : {
"font_size" : 15,
"uuid" : "32799482-CFED-474F-8B25-7E044E478CDA",
"name" : "button1",
"corner_radius" : 40,
"border_width" : 1,
"action" : "button_action",
"class" : "Button",
"frame" : "{{80, 104}, {80, 32}}",
"title" : "Start"
},
"frame" : "{{29, 175}, {80, 80}}",
"selected" : false,
"nodes" : [
]
},
{
"class" : "Button",
"attributes" : {
"font_size" : 15,
"uuid" : "C0300CD9-B1C7-49A8-ADF2-B16EF023A146",
"name" : "button2",
"corner_radius" : 40,
"border_width" : 1,
"action" : "button_action",
"class" : "Button",
"frame" : "{{159, 120}, {80, 32}}",
"title" : "Stop"
},
"frame" : "{{145, 175}, {80, 80}}",
"selected" : false,
"nodes" : [
]
},
{
"class" : "Button",
"attributes" : {
"font_size" : 15,
"uuid" : "0666B9DE-5266-452A-A2CF-F583594B57A8",
"name" : "button3",
"corner_radius" : 40,
"border_width" : 1,
"action" : "button_action",
"class" : "Button",
"frame" : "{{159, 120}, {80, 32}}",
"title" : "Reset"
},
"frame" : "{{275, 175}, {80, 80}}",
"selected" : false,
"nodes" : [
]
}
]
}
]
import ui, scene
class TimerView(ui.View):
class TimerScene(scene.Scene):
def update(self):
self.view.superview.update()
def create_sceneview(self):
scene_view = scene.SceneView()
scene_view.width = 0
scene_view.height = 0
scene_view.frame_interval = self.frame_interval
scene_view.scene = TimerView.TimerScene()
return scene_view
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.frame_interval = kwargs.get('frame_interval', 1)
self.add_subview(self.create_sceneview())
@property
def start_time(self):
return self.subviews[0].scene.t
def draw(self):
pass
def update(self):
self.set_needs_display()
if __name__ == '__main__':
from time import localtime
class DigitalClock(TimerView):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def draw(self):
t = localtime()
ui.draw_string("{:02}:{:02}:{:02}".format(
t.tm_hour, t.tm_min, t.tm_sec),
font=('Helvetica', 20),
rect=(100, 100,0,0),
alignment=ui.ALIGN_CENTER)
v = DigitalClock(frame=(0,0,300, 300))
v.present('sheet')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment