How To Make A Digital Clock In Python

In this article, we will learn about,

  • Why Python?
  • What is Tkinter?
  • How to make a digital clock in Python 
  • Additional Article References

Why Python? 

Python programming language is one of the fastest-growing programming languages in the world. Python is interpreted as an object-oriented, high-level programming language with dynamic semantics with clear syntax, which enables users to learn easily as compared to other programming languages. One of the premium reasons is that it offers not just very active community support but also modules and packages, which enables development very easily, efficiently.

What is Tkinter?

The Tkinter is a Python Interface to build Graphics User Interface ( GUI ) Toolkit. Tkinter enables users to build GUI applications in Python, python with Tkinter enables users to build create GUI in a fast and easy way. Tkinter is implemented as a Python wrapper around a complete Tcl interpreter embedded in the Python interpreter.

How to make a digital clock in Python?

The first and very important step is to import the library and packages, thus we do so by importing Tkinter, label, and Tk. Also we now, here the functionality by importing time in the program.

from tkinter import Label, Tk 
import time

After that we introduce the Tk() to the app window, also here we define the app title, and app geometry, with more important to understand to apply the resizable property as follows.

app_window = Tk() 
app_window.title("C# Corner Digital CLock") 
app_window.geometry("580x200") 
app_window.resizable(1,1)

As we do the above code, now we do the font of text and background with foreground capability and definition to each of these along with the border width.

text_font= ("Lato", 100, 'bold')
background = "red"
foreground= "white"
border_width = 25

Then, we add labels and other things to the app window, with other properties along with grid definitions as row and column values.

label = Label(app_window, font=text_font, bg=background, fg=foreground, bd=border_width) 
label.grid(row=0, column=1)

Then we add the clock definition and introduce the format as H:M:S and after that, we call the main loop.

def dclock(): 
   time_live = time.strftime("%H:%M:%S")
   label.config(text=time_live) 
   label.after(200, dclock)

dclock()
app_window.mainloop()

The full code,

from tkinter import Label, Tk 
import time
app_window = Tk() 
app_window.title("C# Corner Digital CLock") 
app_window.geometry("580x200") 
app_window.resizable(1,1)

text_font= ("Lato", 100, 'bold')
background = "red"
foreground= "white"
border_width = 25

label = Label(app_window, font=text_font, bg=background, fg=foreground, bd=border_width) 
label.grid(row=0, column=1)

def dclock(): 
   time_live = time.strftime("%H:%M:%S")
   label.config(text=time_live) 
   label.after(200, dclock)

dclock()
app_window.mainloop()

The output

How To Make A Digital Clock in Python

Additional Article References,