Make a Digital Clock From Scratch in Python
Make a Digital Clock From Scratch in python - mini project
Make a Digital Clock From Scratch in python
Today I will show you how you can create a digital clock using python and the Tkinter library.
Let's get started:
First write starter code and import statement
from tkinter import *
import time
from PIL import Image
from PIL.ImageTk import PhotoImage
root = Tk()
root.title('Digital Clock - Sailendra')
root.geometry("600x650")
root.configure(bg="#4cf5ba")
# Body of the program
root.mainloop()
Create a function for clock timer
#A function for clock
def clock():
hour = time.strftime("%I")
minute = time.strftime("%M")
second = time.strftime("%S")
am_pm = time.strftime("%P")
clock_label.config(text=hour + ":" + minute + ":" + second + " " + am_pm)
clock_label.after(1000, clock) # it call clock function in every seconds
Create a label for adding a background image
#Background image
image = Image.open("img/digital_clock.png")
resized = image.resize((500, 500), Image.ANTIALIAS)
new_image = PhotoImage(resized)
image_label = Label(root, image=new_image, bg="#4cf5ba")
image_label.grid(row=0, column=0,pady=40, padx=(40, 0))
Create a label text to display hour, minute, and second
#Create label to display watch's text
clock_label = Label(root, text="10:20:20 Am", font="Helvetica 46 bold", bg='white', fg='red')
clock_label.grid(row=0, column=0, padx=(40, 0))
Call a
clock()
function
#Calling the function
clock()
Create a label for footer image/signature
#Footer image
footer_image = Image.open("img/sailendra.png")
footer_resized = footer_image.resize((120, 70), Image.ANTIALIAS)
footer_new_image = PhotoImage(footer_resized)
footer_image_label = Label(root, image=footer_new_image, bg='#4cf5ba')
footer_image_label.grid(row=1, column=0, pady=(0, 20), sticky=W)
We made it.
If you stuck somewhere then feel free to reach out to me at TWITTER