Create a Simple Digital Clock in Python using Tkinter

Digital clock in python using tkinter library - PySeek

Introduction

There are two types of clocks, analog, and digital. Analog clocks are made with only hardware components. But in the digital clock, a display is required and a program always runs in the background and shows the current time.

In this tutorial, we will create a simple Digital Clock in Python using the Tkinter library.

Requirements

Since our clock will display the current time in a GUI window, we need to install the Tkinter library to manage its graphics.

Install tkinter: pip install tk

The Code


'''Digital Clock in Python'''
import time
from tkinter import *

def show_time():
current_time = time.strftime('%H:%M:%S %p | %a')
display.config(text = current_time)
# ms: int, func: None = ...
display.after(100, show_time)

if __name__ == "__main__":
window = Tk()
window.title('Digital Clock')
display = Label(window, font=('Helvetica', 34),
bg = 'gray8', fg = 'yellow')
# anchor must be n, ne, e, se, s, sw, w, nw, or center
display.pack(anchor='e')
show_time()
window.mainloop()

Output


Explanation of the Code

The graphical user interface of the clock has been managed using the Tkinter library. At the very first, we imported tkinter and time module.

The main function


if __name__ == "__main__":
window = Tk()
window.title('Digital Clock')
display = Label(window, font=('Helvetica', 34),
bg = 'gray8', fg = 'yellow')
# anchor must be n, ne, e, se, s, sw, w, nw, or center
display.pack(anchor='e')
show_time()
window.mainloop()

Here we called the Tk() function to create a GUI window, then gave it a name, 'Digital Clock'. After that, we declared a label widget, named 'display' to show the current time in it as a string format. We set the background color 'gray8', and text color 'yellow'.

display.pack(anchor='e'): This line of the code will show the 'display' label at the east corner of the GUI window. There are many more options. Examples: n(north), ne(north-east), e(east), se(south-east), s(south), sw(south-west), w(west), nw(north-west), and center.

Function to show the current time


def show_time():
current_time = time.strftime('%H:%M:%S %p | %a')
# tkinter label.config text
display.config(text = current_time)
# ms: int, func: None = ...
# tkinter label.after 200 ms
display.after(200, show_time)

strftime(): It converts a time tuple to a string according to a format specification. %H, and %M shows hour, minutes respectively, %S shows second, %p shows PM or AM, and %a shows the weekday name.

There are two more options. %m, and %Y. These are used for showing the month name and year respectively.

Then we configure the current time into the display label by calling the .config() method. The after() method is used to call the function show_time() once, after a given time. I've used 200 ms(millisecond) for the next call.

Summary

In this tutorial, we build a Digital Clock using Python Tkinter library. It was a very easy task but can help you to add this simple feature to a big project.

For any queries related to this topic, please leave your comment below. You will get an immediate response.

Thanks for reading!💙

PySeek

Subhankar Rakshit

Meet Subhankar Rakshit, a Computer Science postgraduate (M.Sc.) and the creator of PySeek. Subhankar is a programmer, specializes in Python language. With a several years of experience under his belt, he has developed a deep understanding of software development. He enjoys writing blogs on various topics related to Computer Science, Python Programming, and Software Development.

Post a Comment (0)
Previous Post Next Post