This program displays a digital clock with the current time. Rather than render numeric characters directly, the sevseg.py module from Project 64, “Seven-Segment Display Module,” generates the drawings for each digit. This program is similar to Project 14, “Countdown.”
The Program in Action
When you run digitalclock.py, the output will look like this:
The digital clock program looks similar to Project 14, “Countdown.” Not only do they both import the sevseg.py module, but they must split up the multiline strings returned by sevseg.getSevSegStr() with the splitlines() method. This allows us to put a colon made of asterisks in between the digits for the hour, minute, and second sections of the clock. Compare this code with the code in Countdown to see how it is similar and how it is different.
1. """Digital Clock, by Al Sweigart [email protected]
2. Displays a digital clock of the current time with a seven-segment
3. display. Press Ctrl-C to stop.
4. More info at https://en.wikipedia.org/wiki/Seven-segment_display
5. Requires sevseg.py to be in the same folder.
6. View this code at https://nostarch.com/big-book-small-python-projects
7. Tags: tiny, artistic"""
9. import sys, time
10. import sevseg # Imports our sevseg.py program.
13. while True: # Main program loop.
14. # Clear the screen by printing several newlines:
15. print('\n' * 60)
17. # Get the current time from the computer's clock:
18. currentTime = time.localtime()
19. # % 12 so we use a 12-hour clock, not 24:
20. hours = str(currentTime.tm_hour % 12)
21. if hours == '0':
22. hours = '12' # 12-hour clocks show 12:00, not 00:00.
23. minutes = str(currentTime.tm_min)
24. seconds = str(currentTime.tm_sec)
26. # Get the digit strings from the sevseg module:
27. hDigits = sevseg.getSevSegStr(hours, 2)
28. hTopRow, hMiddleRow, hBottomRow = hDigits.splitlines()
30. mDigits = sevseg.getSevSegStr(minutes, 2)
31. mTopRow, mMiddleRow, mBottomRow = mDigits.splitlines()
33. sDigits = sevseg.getSevSegStr(seconds, 2)
34. sTopRow, sMiddleRow, sBottomRow = sDigits.splitlines()
36. # Display the digits:
37. print(hTopRow + ' ' + mTopRow + ' ' + sTopRow)
38. print(hMiddleRow + ' * ' + mMiddleRow + ' * ' + sMiddleRow)
39. print(hBottomRow + ' * ' + mBottomRow + ' * ' + sBottomRow)
41. print('Press Ctrl-C to quit.')
43. # Keep looping until the second changes:
44. while True:
46. if time.localtime().tm_sec != currentTime.tm_sec:
48. except KeyboardInterrupt:
49. print('Digital Clock, by Al Sweigart [email protected]')
50. sys.exit() # When Ctrl-C is pressed, end the program.
Exploring the Program
Try to find the answers to the following questions. Experiment with some modifications to the code and rerun the program to see what effect the changes have.
What happens if you change time.sleep(0.01) on line 45 to time.sleep(2)?
What happens if you change the 2 on lines 27, 30, and 33 to 1?
What happens if you delete or comment out print('\n' * 60) on line 15?
What error message do you get if you delete or comment out import sevseg on line 10?