Coders Packet

Digital Clock using Tkinter in Python

By Soumyajit Garai

The purpose of this project is to build a Digital Clock using Tkinter in Python language which will display the time in 12-hour clock format and the corresponding meridian.

Digital Clock using Tkinter in Python language:

In this project, we will build a Digital Clock using the Tkinter module in Python. The time will be displayed in "hours" and "minutes" in 12-hour clock format. The corresponding meridian will be displayed too. Moreover, the "seconds" will be displayed on the top right corner of the "minutes". The code for the Digital Clock is attached to this project. Just open the file "digiclock.py" and run it to view the clock.

The font style "RadioLand" needs to be installed in the system to get the exact output as shown in the end. Else, you can use your desired font style. Just change "font=("radioland", ..., ...)" to "font=("YourDesiredFont", ..., ....)" in each case.

Modules to be imported: "tkinter" and "time"

from tkinter import*
import time

Then add the following code which will build the Digital Clock:

root = Tk()
root.title("DIGITAL CLOCK")
root.geometry("1920x1080+0+0")
root.config(bg = "black")

def digiclock():
    h = str(time.strftime("%H"))
    m = str(time.strftime("%M"))
    s = str(time.strftime("%S"))
 
    if int(h) >= 12 and int(h) < 24 and int(m) >= 0:
        meridian.config(text = "PM")
    else:
        meridian.config(text = "AM")
    if int(h) > 12:
        h = str((int(h) - 12))
    elif int(h) == 0:
        h=str(12)

    hour.config(text = h)
    minute.config(text = m)
    second.config(text = s)
    hour.after(200, digiclock)

hour = Label(root, text = "12", font = ("radioland", 100, "bold"), bg = "black", fg = "#39ff14")
hour.place(x = 520, y = 300, width = 200, height = 150)

colon = Label(root, text = ":", font = ("radioland", 80, "bold"), bg = "black", fg = "#39ff14")
colon.place(x = 730, y = 340, width = 50, height = 70)

minute = Label(root, text = "12", font = ("radioland", 100, "bold"), bg = "black", fg = "#39ff14")
minute.place(x = 780, y = 300, width = 200, height = 150)

second = Label(root, text = "12", font = ("radioland", 20, "bold"), bg = "black", fg = "#39ff14")
second.place(x = 943, y = 260, width = 50, height = 50)

meridian = Label(root, text = "PM", font = ("radioland", 20, "bold"), bg = "black", fg = "#39ff14")
meridian.place(x = 967, y = 410, width = 60, height = 30)


digiclock()
root.mainloop()

Output:

Snapshot of the output

Thank You!

Download project

Reviews Report

Submitted by Soumyajit Garai (soumyag27)

Download packets of source code on Coders Packet