Digital clock using Python with Source code

  Step by Step Procedure to create Digital clock using Python

Hello friends how are you, Today in this post "Digital clock using Python" i am going to teach you how you can create a Digital Clock in Python using very simple lines of code. If you are a computer science students or teacher and want to learn something interesting in programming then just go through this post. 

Now  i am going to explain it step by step so just go through this post to understand this completely.

If you want to understand this through video then watch this video i have explained it step by step live

Step 1: Install Python : Click here to watch a video on how to install python or Open any browser and type Download Python and click the first link you will get official website of python here you will get a Download button and after clicking on this button you will get exe of latest python version just install it into your system. 

Step 2: Install Pycharm | Create Project | Install LibraryClick here to watch a single video on How to install Pycharm | Create Project | Install Library  or To install Pycharm IDE Open any browser and type Download Pycharm and click the first link you will get official website of Pycharm  here you will get a black download button and after clicking on this button you will get an exe of Pycharm , just install it into your system. If you are facing problem to install then watch the above video i have explained step by step.

Step 3: Create Project : Open Pycharm  click on File which is in top left of screen and select New Project. Then you will get a screen. Here first you need to select directory of your project and then type a name for your project like "DigitalClock" after directory name, and at last click the create button to create this project. 

Step 4: Create Python file: To create a python file for coding in your project just right click on project name "DigitalClock" and select new and click on Python File , you will get popup screen in which you have to type a name like "Program" for python file and press enter, it will create a python file with name Program.py inside the project DigitalClock. 


Step 5: Code ExplanationTo explain the code i have divided it into four part and now i am going to explain each part one by one

import library: Here in this step we will import the needed library that will be used to create Digital Clock.

from  tkinter import *
from  time import strftime

Here as you can see that there are two libraries are imported in which tkinter library is using to create a GUI window and time library is using to get the current system time.

Function to get Current Time: Here in this step we will create a function that will return the current system time.

def time():
    str=strftime("%H : %M : %S %p")
    lbl.config(text=str)
    lbl.after(1000,time)

Here i have defined a function with name time(). strftime() function will return the current system time in the given format and it will store into variable str. In the third line the i have passed the current time to lbl, and the last line refresh the text in Label after every second.

Create GUI Window :Here in this step will will create a window to display the current time in Digital Format.
myWindow=Tk()
myWindow.title("Digital Clock")
lbl=Label(myWindow,font=("Algerian",100),background="black",foreground="cyan")
lbl.pack()

Here in the first line i have created a myWindow variable for displaying a GUI window. In the second line title is set in the window and in the third line a label is created . The background color of Label is black, text color is cyan , font-family is Algerian and the font size of Label is 100. The variable name for the label is lbl.

Function call and Run Application: Here in this step we will call the function to get current time and will use mainloop() function to Run Application.
time()
mainloop()

Here in the first line time() is called to get time and at last mainloop() function is called to Run the Application.

Complete Python code:  Here is the complete code for this project/program you just need to type this code into your python file or you can copy this code for your personal use.

#import library
#tkinter library to create GUI Window
from  tkinter import *
#time library to get current system time
from  time import strftime
#define function to get current time
def time():
    #get current time in given format
    str=strftime("%H : %M : %S %p")
    #passing time to label
    lbl.config(text=str)
    #refresh label every second
    lbl.after(1000,time)
#creating GUI window  
myWindow=Tk()
#setting title to window
myWindow.title("Digital Clock")
#creating label to dispaly digital clock
lbl=Label(myWindow,font=("Algerian",100),background="black",foreground="cyan")
lbl.pack()
#calling time function
time()
#Calling function to Run Application
mainloop()

Here you just need to type this your code into program.py file or you can copy this code for your personal use only.

Step 6: Run Code: To run this code just right click on the coding area and click on Run Program and you will get a output screen like below.

Digital clock using Python

I hope now you can  create "Digital clock using Python". If you have any doubt regarding this post  or you want something more in this post then let me know by comment below i will work on it definitely.
 

Request:-If you found this post helpful then let me know by your comment and share it with your friend. 
 
If you want to ask a question or want to suggest then type your question or suggestion in comment box so that we could do something new for you all. 

If you have not subscribed my website then please subscribe my website. Try to learn something new and teach something new to other. 

If you like my post then share it with your friends. Thanks😊Happy Coding.

Post a Comment

0 Comments