22.4 C
New York
Sunday, December 22, 2024
HomeProgrammingPythonHow To Make A Digital Clock In Python Using Tkinter

How To Make A Digital Clock In Python Using Tkinter

In this tutorial, you will learn how to make a digital clock in Python using Tkinter. Download and run the Python script to see digital clock in action.

In this tutorial, you will learn how to make a digital clock in Python. Before moving ahead, if you are new to Python programming, check out these recommended books.

Using the Python program in this tutorial, you will create a digital clock and customize its appearance.

For example, you can change the font size and style. You can also change the background and foreground of the digital clock.

Also ReadHow To Use Python For Browser Games Development?

How To Make A Digital Clock In Python Using Tkinter

We will use its Tkinter module to create a digital clock using Python. Tkinter is the most commonly used GUI programming unit for Python.

Using Tkinter, you can develop excellent software applications with a clean interface.

Let’s now see the Python code to make a digital clock in Python.

# import tkinter module 
from tkinter import * 
from tkinter.ttk import *
  
# import strftime function to retrieve system's time 
from time import strftime 
  
# creating tkinter window 
root = Tk() 
root.title('Clock') 
  
# This function is used to  
# display time on the label 
def time(): 
    string = strftime('%H:%M:%S %p') 
    lbl.config(text = string) 
    lbl.after(1000, time) 
  
# Styling the label widget so that clock 
# will look more attractive 
lbl = Label(root, font = ('calibri', 40, 'bold'), 
            background = 'purple', 
            foreground = 'white') 
  
# Placing clock at the centre 
# of the tkinter window 
lbl.pack(anchor = 'center') 
time() 
  
mainloop()

Download the Python File.

Let’s now understand how this Python code creates a colorful digital clock.

1. Importing modules and creating a Tkinter window

Import the Tkinter module and strftime module in your Python program. We need the Tkinter module to create and stylize the clock and strftime package to get the system’s time.

Use this code to create a Tkinter Window.

root = Tk() 
root.title('Clock')

2. Creating a label and displaying the time on it

Using a label, we create a time() function to display time on the Tkinter’s window.

def time(): 
    string = strftime('%H:%M:%S %p') 
    lbl.config(text = string) 
    lbl.after(1000, time)

3. Customizing the appearance of the clock

In this next step, we will customize the clock’s appearance by changing the font size, style, background color, and foreground color.

Here’s the code to do it.

lbl = Label(root, font = ('calibri', 40, 'bold'), 
            background = 'purple', 
            foreground = 'white')

4. Placing the digital clock at the center

Finally, we will change the alignment of the clock to the center using this code.

lbl.pack(anchor = 'center') 
time()

Output

Here’s how your digital clock will look when you execute the above Python code.

digital clock in python

Other Python programming tutorials:

Final Words

In this tutorial, you learned how to make a digital clock in Python using Tkinter. Next time, we will explore how to create a simple alarm or reminder application with Windows notification support.

Let us know if this tutorial is helpful in the comments section below. If you want specific tutorials, please suggest them by writing to me at codeitbro@gmail.com.

Happy coding, and before you get busy, check out these other programs for beginners. Also, check out these Python programming memes to lighten up your mood. 🙂

Himanshu Tyagi
Himanshu Tyagi
Hello Friends! I am Himanshu, a hobbyist programmer, tech enthusiast, and digital content creator. With CodeItBro, my mission is to promote coding and help people from non-tech backgrounds to learn this modern-age skill!
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
RELATED ARTICLES
0
Would love your thoughts, please comment.x
()
x