-
Notifications
You must be signed in to change notification settings - Fork 0
/
Digital Clock.py
75 lines (66 loc) · 1.58 KB
/
Digital Clock.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# http://www.codeskulptor.org/#user45_boUV3WSIyL_4.py
# Yu Sheng 18/09/18
import simplegui
second = 0
minute = 0
hour = 0
# Update timer
def update_timer():
global second
second += 1
# Clock Format
def clock_format():
global second
global minute
global hour
global part3
if(second > 59):
minute += 1
second = 0
if(minute > 59):
hour += 1
minute = 0
if(hour > 23):
hour = 0
if(hour < 10):
part1 = "0" + str(hour);
elif(hour < 24):
part1 = str(hour);
if(minute < 10):
part2 = "0" + str(minute);
elif(minute < 60):
part2 = str(minute);
if(second < 10):
part3 = "0" + str(second);
elif(second < 60):
part3 = str(second);
return part1 + ":"+ part2;
# Set Hour
def set_hour():
global hour
hour += 1;
# Set Minute
def set_minute():
global minute
minute += 1;
# Reset Second
def reset_second():
global second
second = 0;
timer.stop()
timer.start()
# Handler to draw on canvas
def draw(canvas):
canvas.draw_text(clock_format(), [90,112], 48, "White")
canvas.draw_text(part3, [200,112], 20, "White")
# Create a frame and assign callbacks to event handlers
frame = simplegui.create_frame("Home", 300, 200)
frame.add_button("Set Hour", set_hour)
frame.add_button("Set Minute", set_minute)
frame.add_button("Reset Second", reset_second)
frame.set_draw_handler(draw)
timer = simplegui.create_timer(1000, update_timer)
# Every 1000 mili sec call timer_handler
# Start the frame animation
frame.start()
timer.start()