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. Additionally, you can also change the background and foreground of the digital clock.
Also Read: How To Use Python For Browser Games Development?
How To Make A Digital Clock In Python Using Tkinter
To create a digital clock using Python, we will use its Tkinter module. Tkinter is the most commonly used GUI programming unit for Python. Using Tkinter, you can develop cool 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()
Let’s now understand how this Python code works to create a colorful digital clock.
1. Importing modules and creating a Tkinter window
Import 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 time on it
Moving forward, we will create a time() function to display time on the Tkinter’s window using a label.
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 as soon as you execute the above Python code.
Other Python programming tutorials:
- Sending Emails Using Python With Image And PDF Attachments
- Working With Python 3 Lists | Learn By Examples
- Dictionary In Python 3 | Learn With Examples
- How To Make A Simple Python 3 Calculator Using Functions
- Python 3 Program To Convert Celsius To Fahrenheit
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 reminders application with Windows notification support.
Do let us know if you find this tutorial useful in the comments section below. If you want specific tutorials, then please suggest by writing to me at [email protected].
Happy coding, and before you get busy, also check out these other programs for beginners. Also, check out these Python programming memes to lighten up your mood :)
- Python 3 Program To Multiply Two Matrices
- Python 3 Program To Add Two Matrices
- Python 3 Program to Check Armstrong Number
- Python 3 Program to Find the Sum of Natural Numbers
- Python 3 Program To Find The Factorial Of A Number
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!