In this post, I’ll show you how to create and design digital clock window using Python. This is an easy project to get started with Tkinter, a built-in package that comes with Python and time module. Tkinter is basically a GUI package. Tkinter takes its name from the Tk interface. When combined with Tkinter, Python provides a quick and easy way to build GUI applications. Tkinter offers a powerful object-oriented interface for the Tk-GUI-Toolkit. It has great features that can be used to create simple applications and today we are going to use it to create our digital clock.
The great thing about building your own digital watch is that you can customize it however you want.
Requirements for creating a digital clock in Python
First we need to install the Tkinter module. If this module is not already installed on your system, you can install it using the pip package manager:
pip install tkinter
Coding the Digital Clock in Python
We are going to use the tkinter module and the time module to build our clock. The Time module offers various options for calling up the time. In this we’ll use strftime() to parse the current time in the hour: minutes: seconds format. We will use geometry() to indicate the dimension of the window being displayed and we are using mainloop() to prevent the displayable window from exiting quickly.
#First import all which are required
import sys
from tkinter import *
#import time library to obtain current time
import time
#create a function timing and variable current_time
def timing():
#display current hour,minute,seconds
current_time = time.strftime("%H : %M : %S")
#configure the clock
clock.config(text=current_time)
#clock will change after every 200 microseconds
clock.after(200,timing)
#Create a variable that will store our tkinter window
twindow=Tk()
#define size of the window
twindow.geometry("650x400")
#create a variable clock and store label
#First label will show time, second label will show hour:minute:second, third label will show the top digital clock
clock=Label(twindow,font=("verdana",70,"bold"),bg="green")
clock.grid(row=2,column=2,pady=25,padx=100)
timing()
#create a variable for digital clock
digital=Label(twindow,text="Digital Clock",font="verdana 25 bold")
digital.grid(row=0,column=2)
nota=Label(twindow,text="hours minutes seconds",font="verdana 15 bold")
nota.grid(row=3,column=2)
twindow.mainloop()