Python program to make digital clock using tkinter Novice Koder

In this tutorial, I am going to build our own digital clock with python & Tkinter GUI

Prerequisites:

-> Python Functions

->Tkinter basic 

->Time Module



from tkinter import *
import time as t

root = Tk()
root.title("DIGITAl CLOCK")
root.geometry("900x250")
root.configure(bg="white")
def time():
    dt = t.strftime("%A, %d. %B %Y \n %I:%M:%S%p")
    label.config(text=dt)
    label.after(1000,time)

label=Label(root,font=("Digital-7",80),bg="white",fg="black", )
label.pack()
time()
mainloop()


First, create a python file, you can name anything. I am naming digital_clock.py

Importing the necessary modules

from tkinter import *
from time import t


Now we need to create UI for clock

root = Tk()


Next we set the title for clock


root.title('Digital Clock')


Now. let's create the function for our clock. I name it as time(). we will use strftime method to get the day & time and store it inside a string. Let's the name the string as dt. Now it's time to provide the time format.


def time():
    dt = t.strftime("%A, %d. %B %Y \n %I:%M:%S%p")

Next step, to set the label using config method.

label.config(text = dt)

We can call our clock function and use the after method to do the same. We would like to call it every second, so that will be our first parameter.

label.after(1000, clock)

Let's design the Label

label = Label(root,font=("Digital-7",80),background="white",foreground="black")

Moving forward let's call our time function()  and at the end we will call mainloop(). That's it and we are done. Run your program & it will look like this given below!


python digital clock - Novice Koder