Create A Digital Clock Using Python

0
468

Introduction To Digital Clock Using Python

The great part of creating your own GUI apps is that you can customize them however you want. From text font to background color, all features are available for customization. In this article, I will take you through how to create a digital clock with Python.

In this section, I will show you how to create a digital clock. This is a simple task to get started with the Tkinter library in Python, which is a built-in package that comes with Python. Tkinter has some cool features that can be used to build simple apps

Now let’s see how to create a digital clock GUI application with Python. I will first start with importing the libraries:

from tkinter import Label, Tk 
import time

Now let’s define the title and size of our application. Note that in the code below I will set both the height and width of the resizable function as True(1,1) if you want a fixed window and don’t want to maximize or minimize the output you can set it to False(0,0):

app_window = Tk() 
app_window.title("Digital Clock") 
app_window.geometry("420x150") 
app_window.resizable(1,1)

Now here I will define the font of the time and its color, its border width, and the background color of the digital clock:

text_font= ("Boulder", 68, 'bold')
background = "#f2e750"
foreground= "#363529"
border_width = 25

Now here I will combine all the elements to define the label of the clock application:

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

Now let’s define the main function of our digital clock. Here I will set the text of the label as the real-time:

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

Now let’s run and see our output:

Digital Clock Using Python

Complete Code:

from tkinter import Label, Tk 
import time
app_window = Tk() 
app_window.title("Digital Clock") 
app_window.geometry("420x150") 
app_window.resizable(1,1)

text_font= ("Boulder", 68, 'bold')
background = "#f2e750"
foreground= "#363529"
border_width = 25

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

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

digital_clock()
app_window.mainloop()

I hope you liked this article on how to create a digital clock with the Python programming language. You Can Also Read About Best Python Libraries For Machine Learning.

LEAVE A REPLY

Please enter your comment!
Please enter your name here