Python Real Life Examples

How to create a Digital Clock in Python With Tkinter

How to Make a Digital Clock with Python and Tkinter:

We will now make a digital Clock. Here is its Demo.

Importing Modules

Import the following modules.

from tkinter import *
from tkinter.ttk import * 
from time import strftime
import platform

Here we have imported Tkinter, DateTime, and platform(for determining Operating System)

Making a Tkinter Window

We will now make a Tkinter window.

main=Tk()
main.title("Digital Clock in Python")
main.geometry('1000x250')

We have made a simple Tkinter window here. We have declared the title “Clock”. and set its size to ‘1000X250’

Creating Clock

We will now Create a Digital Clock.

Making a Clock Function

We will Now make a clock function that controls the Time and Date Labels.

def clock():
    tick=strftime('%H:%M:%S:%p')
    clock_label.config(text=tick)
    clock_label.after(1000,clock)
clock_label=Label(main,font=('sans',90),background='green',foreground='white')

In this Function, date and time are taken from the DateTime module, and then it is converted into PM and AM Time. You Have to add this function below Tkinter initialization and above Notebook initialization.

clock_label.pack(anchor='center')
clock()
mainloop()

Whole Sourcecode at once

from tkinter import *
from tkinter.ttk import * 
from time import strftime
main=Tk()
main.title("Digital Clock in Python")
main.geometry('1000x250')
def clock():
    tick=strftime('%H:%M:%S:%p')
    clock_label.config(text=tick)
    clock_label.after(1000,clock)
clock_label=Label(main,font=('sans',90),background='green',foreground='white')
clock_label.pack(anchor='center')
clock()
mainloop()

Now You Can Run Your code simply from Terminal or Command Line or Your IDE/Editor. It will Work.

Important Notice for college students

If you’re a college student and have skills in programming languages, Want to earn through blogging? Mail us at geekycomail@gmail.com

For more Programming related blogs Visit Us Geekycodes. Follow us on Instagram.

Leave a Reply

%d bloggers like this: