Creating a Simple Digital Clock in Python

In this tutorial we will create a Simple Digital Clock Using Python. Python is a widely used advanced-level programming language for a general technique to the developer. Beginners find the clean syntax and indentation structure easy to learn because of it's less semicolon problem. You can use this simple program as part of your program.

So let's now do the coding.

Getting started:

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.

Importing Modules

After setting up the installation and the database, run the IDLE and click the file and then the new file. After that, a new window will appear containing a black file this will be the text editor for the python.

  1. from tkinter import *
  2. import time

Setting up the Main Frame

After importing the modules, we will now then create the main frame for the application. To do that just copy the code below and paste it inside the IDLE text editor.

  1. root = Tk()
  2. root.title("Sourcecodester")
  3. width = 600
  4. height = 400
  5. screen_width = root.winfo_screenwidth()
  6. screen_height = root.winfo_screenheight()
  7. x = (screen_width/2)- (width/2)
  8. y = (screen_height/2) - (height/2)
  9. root.geometry("%dx%d+%d+%d" % (width, height, x, y))
  10. root.resizable(0, 0)
  11. root.config(bg="light blue")

Designing the Layout

After creating the Main Frame we will now add some layout to the application. Just kindly copy the code below and paste it inside the IDLE text editor.

  1. #===========================================FRAMES========================================
  2. Top = Frame(root, width=600, bd=1, relief=SOLID)
  3. Top.pack(side=TOP)
  4. Mid = Frame(root, width=600)
  5. Mid.pack(side=TOP, expand=1)
  6.  
  7.  
  8. #===========================================LABEL WIDGET==================================
  9. lbl_title = Label(Top, text="Python: Simple Digital Clock", width=600, font=("arial", 20))
  10. lbl_title.pack(fill=X)
  11.  
  12. clock = Label(Mid, font=('times', 50 , 'bold'), fg="green", bg="light blue")
  13. clock.pack()

Creating the Function

This where the code that handle the clock function. When the application run, the clock is automatically displayed.

  1. #===========================================METHODS=======================================
  2. def tick():
  3. setTime = time.strftime('%I: %M %S %p')
  4. clock.config(text=setTime )
  5. clock.after(200, tick)

Initializing the Application

After finishing the function save the application as 'index.py'. This function will run the code and check if the main is initialize properly. To do that copy the code below and paste it inside the IDLE text editor.

  1. #===========================================INITIALIZATION================================
  2.  
  3. if __name__ == '__main__':
  4. tick()
  5. root.mainloop()

There you have it we successfully created a Simple Digital Clock Using Python. I hope that this simple tutorial helps you with what you are looking for. For more updates and tutorials just kindly visit this site.

Enjoy Coding!!

Add new comment