How to create a digital watch in Python

In this post, you are going to learn how to create a digital watch in Python.

The modules which we are going to use are Tkinter module and Time module.

To install Tkinter – Open Command Prompt and write pip install tkinter. If you are having Python 3.1, then you need not to install it as from 3.1 onwards, it is the part of the standard python distribution.

Pre-requisites

Basics of Tkinter, Functions in Python, Modules in Python.

Firstly, we will import the sys module which provides information about constants, functions, and methods of the interpreter. Then, we want to import Tkinter. So doing from tkinter import * means that we want to use the original widgets. Time module is imported which provides functionality other than representing time in code, such as objects, numbers, and strings. Also, waiting during code execution and measuring the efficiency of our code are the features of the Time module.

We will be defining a function DClock() in which the strftime() will be used in order to get the local time from the PC.

The Label widgets are used to give the title to the app window and to give some styling to it.

Program: Create a digital watch in Python

import sys #to import system files
from tkinter import *   #whole module is imported
import time #importing local time

#Used to display time on the label
def DClock():
    curr_time= time.strftime("%H:%M:%S")
    clock.config(text=curr_time)
    clock.after(100,DClock)

#making window
window=Tk()
window.title('Digital Clock') #adding title to the window

#giving name to our digital clock and styling it
message= Label(window, font=("arial",100,"italic"), text="Time", fg="red")
message.grid(row=0,column=0)
clock= Label(window, font=("times",150,"bold"),fg="black")
clock.grid(row=1,column=0)
DClock()
mainloop() #loop is closed

 

Output

How to create a digital watch in Python

Leave a Reply

Your email address will not be published. Required fields are marked *