Project Digital Clock using Python

Hi, today we are going to program a Project Digital Clock using Python. The Digital Clock here gives time in 12 hour format and shown through seven segment display. This Digital clock project is useful for mid-level programmer and for engineering students. Seven segment display is used in a variety of application. That is why ? It is necessary to know the coding of seven segment display done in this program.

Step by Step Guide : Project Digital Clock using Python

Step 1: Imported module
sys for ctrl+c to end the program by sys.exit() function.
Time for importing current time from the system by time.localtime().
Sevseg (seven segment) It displays hours, minute and seconds in 6 seven segment display. It requires sevseg.py to be in the same folder.
Step 2: While loop is operating in the project till ‘ctrl+c’ is pressed.
Step 3: Clearing the screen.
Step 4: Get the current timing from the computer’s clock.
Step 5: We use a 12-hour clock,% 12 so, not 24.
Step 6: Getting the digit strings from the sevseg module.
Step 7: Displaying the digits.

Source code :Project Digital Clock using Python

print("""Digital Clock, by Archit
  The project displays clock time in digital seven segment display 
  It requires sevseg.py to be in the same folder.
  """)
 
import sys, time
import sevseg # Imports sevseg.py program.

try:
    while True: # Main program loop.
        # Clearing the screen :
        print('\n' * 60)

        # Get the current timing from the computer's clock:
        currentTime = time.localtime()
        #  we use a 12-hour clock,% 12 so, not 24:
        hours = str(currentTime.tm_hour % 12)
        if hours == '0':
            hours = '12' # 12-hour clocks shows 12:00, not 00:00.
        minutes = str(currentTime.tm_min)
        seconds = str(currentTime.tm_sec)

        # Getting 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()

        # Displaying the digits:
        print(hTopRow + ' ' + mTopRow + ' ' + sTopRow)
        print(hMiddleRow + ' * ' + mMiddleRow + ' * ' + sMiddleRow)
        print(hBottomRow + ' * ' + mBottomRow + ' * ' + sBottomRow)
        print()
        print('Press Ctrl-C to quit.')

        # Keep on 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, by Archit')
     sys.exit() # Press Ctrl-C to end the program.

Output:

Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 19:29:22) [MSC v.1916 32 bit (Intel)] on win32 Type “help”, “copyright”, “credits” or “license()” for more information.
>>>
======== RESTART: C:/Users/ARCHIT/Desktop/new python progrm/clock.py ========
Digital Clock, by Archit
The project displays clock time in digital seven segment display
It requires sevseg.py to be in the same folder.

  

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

Press Ctrl-C to quit.

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

Press Ctrl-C to quit.

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

Press Ctrl-C to quit.
Digital Clock, by Archit
>>> 

Conclusion:

Python project is running fine. It is showing different values every second. It is fetching the time from the interrupting computer. Seven segment displays are running fine.

Thank you, for reading and copying down the source code of Project Digital Clock. This program is very useful for programmer and Engineering students.

For more project like this, click the below link:
Python Programs Archives – CS Study

Leave a Comment

Your email address will not be published.