Create a Digital Clock using Python
In this article we will learn how to create a digital clock using tkinter. As we know that Tkinter is to create a variety of GUI(Graphical User Inteface) applications.
Introducing Some About Digital Clock Using Python:
The best part of creating your own Gui apps is that however you want. From text font to background color, all feature are available for customization.
In this section, I will show you how to create a digital clock. we will use tikinter library in python, which is the standard Python interface to the Tcl/Tk GUI toolkit. Tkinter are available on most Unix platforms, including macOS, as well as on Windows System.
Now let's see how to make a digital clock GUI application with Python. we will first start with the importing libraries:
#import all the required libraries first
import sys
from tkinter import *
#import time library to obtain current time
import time
Now let's create a function timing and variable current time and we set clock changing after every 200 microseconds.
def timing():
current_time = time.strftime("%H : %M : %S")
clock.config(text=current_time)
Create a variable that will store our tkinter window and variable clock or store label, The First label will show time, second label will show hour:minute:second, third label will show the top digital clock
root=Tk()
clock=Label(root,font=("times",60,"bold"),bg="skyblue")
clock.grid(row=2,column=2,pady=25,padx=100)
timing()
Let's define the size of the window and creating a variable for Digital Clock
digital=Label(root,text="Python Digital Clock",font="times 24 bold")
digital.grid(row=0,column=2)
nota=Label(root,text="Hours Minutes Seconds",font="times 15 bold")
nota.grid(row=3,column=2)
root.mainloop()
Let's Run the Program and See the Output:
Full Code:
#import all the required libraries first
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
root=Tk()
#define size of the window
root.geometry("600x300")
#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(root,font=("times",60,"bold"),bg="skyblue")
clock.grid(row=2,column=2,pady=25,padx=100)
timing()
#create a variable for digital clock
digital=Label(root,text="Python Digital Clock",font="times 24 bold")
digital.grid(row=0,column=2)
nota=Label(root,text="Hours Minutes Seconds",font="times 15 bold")
nota.grid(row=3,column=2)
root.mainloop()
If you have any problem regarding code or something else Please Comment Down or Go to the contact page.