Python to create a digital clock using Tkinter

PythonServer Side ProgrammingProgramming

Python Tkinter can be used to create all kinds of GUI programs for the web and desktop. In this article we will see how to create a digital clock displaying hour, minute and seconds live.

We use the time module to import the method strftime which displays the time in Hour, minute and seconds format. We create a canvas to hold these values. We refresh the values of strftime after every 200 milli seconds. We define a recursive function to achieve this.

Example

import time
from tkinter import *
canvas = Tk()
canvas.title("Digital Clock")
canvas.geometry("350x200")
canvas.resizable(1,1)
label = Label(canvas, font=("Courier", 30, 'bold'), bg="blue", fg="white", bd =30)
label.grid(row =0, column=1)
def digitalclock():
   text_input = time.strftime("%H:%M:%S")
   label.config(text=text_input)
   label.after(200, digitalclock)
digitalclock()
canvas.mainloop()

Output

Running the above code gives us the following result −


raja
Published on 26-Aug-2020 06:29:46
Advertisements