GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

No menu could be recovered. Follow the blue links in the page.

This view

  • Original page
  • New address

GeezerVison is showing inventwithpython.com — Digital Clock

(Close Ad) Other books by Al Sweigart, free online or available for purchase:
BUY THIS BOOK: No Starch Press (free ebook) | Amazon | Barnes and Noble | Powell's | Bookshop.org | Goodreads | Kobo | Thriftbooks
SUPPORT THE AUTHOR: Patreon or Donate Directly
Prev: Dice Roller | Next: Digital Stream

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:

 __   __       __   __       __   __
|  | |__|  *   __|  __|  *   __| |__
|__|  __|  *   __|  __|  *   __| |__|

Press Ctrl-C to quit.

How It Works

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.

Download Source Code

 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"""
 8.
 9. import sys, time
10. import sevseg  # Imports our sevseg.py program.
11.
12. try:
13.     while True:  # Main program loop.
14.         # Clear the screen by printing several newlines:
15.         print('\n' * 60)
16.
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)
25.
26.         # Get the digit strings from the sevseg module:
27.         hDigits = sevseg.getSevSegStr(hours, 2)
28.         hTopRow, hMiddleRow, hBottomRow = hDigits.splitlines()
29.
30.         mDigits = sevseg.getSevSegStr(minutes, 2)
31.         mTopRow, mMiddleRow, mBottomRow = mDigits.splitlines()
32.
33.         sDigits = sevseg.getSevSegStr(seconds, 2)
34.         sTopRow, sMiddleRow, sBottomRow = sDigits.splitlines()
35.
36.         # Display the digits:
37.         print(hTopRow    + '     ' + mTopRow    + '     ' + sTopRow)
38.         print(hMiddleRow + '  *  ' + mMiddleRow + '  *  ' + sMiddleRow)
39.         print(hBottomRow + '  *  ' + mBottomRow + '  *  ' + sBottomRow)
40.         print()
41.         print('Press Ctrl-C to quit.')
42.
43.         # Keep looping until the second changes:
44.         while True:
45.             time.sleep(0.01)
46.             if time.localtime().tm_sec != currentTime.tm_sec:
47.                 break
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.

  1. What happens if you change time.sleep(0.01) on line 45 to time.sleep(2)?
  2. What happens if you change the 2 on lines 27, 30, and 33 to 1?
  3. What happens if you delete or comment out print('\n' * 60) on line 15?
  4. What error message do you get if you delete or comment out import sevseg on line 10?
Prev: Dice Roller | Next: Digital Stream
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.