This post was last Updated on June 8, 2023 by Himanshu Tyagi to reflect the accuracy and up-to-date information on the page.
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.
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()
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
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()
Here’s how your digital clock will look when 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
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 [email protected].
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 :)