digital-clock-using-python

DIGITAL CLOCK | PYTHON

In this article, we will learn how to create a Digital Clock using simple python script.

INTRODUCTION:

Time plays a major role in everyone’s day to day life. Knowing the importance of time will change your perspective on how time is important for us and what we can do with it to achieve our goals and lead a more meaningful life. Why is this important to us? You can’t purchase time, and time is more valuable than money since you’ll need it in this life for everything.

OUTLINE:

So, in this tutorial, we are going to make a digital clock using a few lines of python code. It is very simple and user-friendly. So, in order to build a digital clock, we need to have knowledge about the following packages in python.

PROJECT PREREQUISITES:

tkinter: tkinter is a profound package in python. Tkinter is a toolkit used in python to make graphical user interface applications. Tkinter is the most commonly used and most basic GUI framework which is used in python programming.

time: time is a python module that provides various kinds of functions related to time representations. There are numerous functions included in time to work with the time and their respective conversions.

Our python project is mainly dependent on these two packages. So, gaining some knowledge about these two packages will enable us to understand the project easily.

So, let’s get into the actual code…

CODE IMPLEMENTATION:

First of all, we don’t need to install the tkinter package as it is in built with python. so, we need to import all the functions which are available in tkinter package like this…

  • from tkinter import *
  • from tkinter.ttk import *
  • from time import strftime 
from tkinter import * 
from tkinter.ttk import *
from time import strftime 

Later, we need to import the strftime from the module time

See also  7 powerful Python Libraries that every programmer should know

from time import strftime 

from time import strftime 

The strftime () method in the time module takes one or more format codes as an argument and returns a formatted string based on it. This function is used to retrieve the time of the particular system.

Now, to create a window for the digital clock, we use the function Tk() from the tkinter package. This function is used to create a tkinter window.

Then we create a function time, which is used to retrieve the time from the respective system and display the time in the window

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

Later, by using the label function, we can design the window and the style of the digital clock according to our preference. Alignment of the widgets in the window is also done by this label function.

lbl = Label(root, font = (‘franklin gothic’, 40, ‘bold’),

            background = ‘black’,

            foreground = ‘white’)

lbl.pack(anchor = ‘center’)

time()  

mainloop()

lbl = Label(root, font = ('franklin gothic', 40, 'bold'), 
            background = 'black', 
            foreground = 'white') 
lbl.pack(anchor = 'center') 
time()  
mainloop()

So, we are all done with the functioning of the code. Now let’s look into the source code and the output window of the code…

Source Code:

This is the total code of our digital clock project. You can copy the source code below…

from tkinter import * 
from tkinter.ttk import *
from time import strftime 
root = Tk() 
root.title('Clock') 
def time(): 
    string = strftime('%H:%M:%S %p') 
    lbl.config(text = string) 
    lbl.after(1000, time) 
lbl = Label(root, font = ('franklin gothic', 40, 'bold'), 
            background = 'black', 
            foreground = 'white') 
lbl.pack(anchor = 'center') 
time()  
mainloop()

On running the above code, we get the time in the tkinter window which is retrieved from the system at the time of interpretation.

See also  50+ Best python projects with source code [2023]

OUTPUT:

Here is the screenshot of the output after running the above python script.

So, that’s it from this tutorial. Hope you find interesting the building of a digital clock using python. You can modify the styles and themes of the tkinter window and the widgets of the clock by adjusting the code according to your preference. so, happy coding!

Important Note:

Note that you can still improve this digital clock by playing with the code and getting to know more about the packages.

Try to make this clock even better and post your output/code in the comments below.

Leave a Comment

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

we provide projects, courses, and other stuff for free. in order for running we use Google ads to make revenue. please disable adblocker to support us.