-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdigitalClock.py
More file actions
42 lines (35 loc) · 1.47 KB
/
digitalClock.py
File metadata and controls
42 lines (35 loc) · 1.47 KB
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
import sys, time
import sevseg
try:
while True: # Main loop
# clear the screen by printing several newlines:
print('\n' * 10)
# Get the current time from the computer's clock:
currentTime = time.localtime()
# %12 so we use a 12-hour clock, not 24:
hours = str(currentTime.tm_hour % 12)
if hours == '0':
hours = '12' # 12-hour clocks show 12:00, not 00:00
minutes = str(currentTime.tm_min)
seconds = str(currentTime.tm_sec)
# Get the digit strings from the sevseg module:
hDigits = sevseg.getSevSegStr(hours, 2)
hTopRow, hMiddleRow, hBottomRow = hDigits.splitlines()
mDigits = sevseg.getSevSegStr(minutes, 2)
mTopRow, mMiddleRow, mBottomRow = mDigits.splitlines()
sDigits = sevseg.getSevSegStr(seconds, 2)
sTopRow, sMiddleRow, sBottomRow = sDigits.splitlines()
# Display the digits:
print(hTopRow + ' ' + mTopRow + ' ' + sTopRow)
print(hMiddleRow + ' * ' + mMiddleRow + ' * ' + sMiddleRow)
print(hBottomRow + ' * ' + mBottomRow + ' * ' + sBottomRow)
print()
print('Press Ctrl-c to quit.')
# Keep looping until the second changes:
while True:
time.sleep(0.01)
if time.localtime().tm_sec != currentTime.tm_sec:
break
except KeyboardInterrupt:
print('Digital Clock')
sys.exit() # When Ctrl-C is pressed, end the program.