Thought leadership from the most innovative tech companies, all in one place.

How To Make A Digital Clock in Python

Python project for beginners and intermediates

image

Before the Big Bang, there was no time. But after the Big Bang, you can import time in Python. I know it’s a bad joke. But to practice Python projects, you can make a lot of time-related projects in Python like a digital clock, countdown timer, alarm clock, and stopwatch. Let’s make a digital clock using Python and Tkinter.

Tkinter is mainly used for UI in Python projects. Before jumping into the code, you have to do some setup for the project. So, let’s start this project with a cup of coffee.

If you are looking for a video tutorial, then it’s here:

Project Setup and Installation

As you know, making a Python project in a virtual environment is a good habit. So, start with a virtual environment.

  • If you are a Linux user, then make a virtual environment using the following command. But make sure that the virtual environment is installed in your system.
virtualenv -p python3.8 clock
cd clock
source bin/activate

If you are a Windows user then make a virtual environment using the following command. But make sure that the virtual environment is installed in your system.

virtualenv clock
clock\scripts\activate
  • Now, make a python file inside this “clock” directory and name it as the file.py.

  • Install the required package.

pip install tk

To use the “tk” library, you must have Tkinter installed in your system. Tkinter is a Python package that mainly deals with the GUI part of a Python project.

To install Tkinter in Linux, you can use the command as shown below.

sudo apt install python3.8-tk

If you are a windows user then, you can install Tkinter at the time of Python installation. You can install it manually too.

Clock using Python

Clock using Python

Code of Digital Clock in Python

file.py

from tkinter import *
from time import strftime

root = Tk()
root.geometry("500x500")
root.resizable(0,0)
root.title('Python Clock')

Label(root,text = 'YOUTUBE - BUG NINZA', font ='arial 20 bold').pack(side=BOTTOM)

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

mark = Label(root,
            font = ('calibri', 40, 'bold'),
            pady=150,
            foreground = 'black')

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

mainloop()
  • Import the package. We are using the system’s time. To retrieve the system’s time, import strftime function.

  • Define a window screen. Here the dimension of the window screen is 500x500. You can’t resize the window screen. After that, set a title for the window screen. It’s optional.

  • I added a footer label on the window screen too. It’s also optional.

  • Define the time function. The format would be H: M: S.

  • Style the clock widget to make it attractive. The font size of the clock string is set to 40 and I added vertical padding of 150. The foreground color is set to “black”.

  • Add a mainloop function at the end.

Our code is complete. Now run the “file.py” file using the following command:

python file.py

After the successful run, you will see something like this on-screen.

Clock using Python

Clock using Python

Congratulations! You successfully built a Python project.

If this article sounds informative to you, then make sure to follow and share it with your geek community.

Here are more Python projects for practice!

Make a Calculator using Python and Tkinter

YouTube Video Downloader using Python and Tkinter

Text to Speech Conversion using Python with gTTS

Capture and Process Video Footage from a Webcam using OpenCV Python

Rock, Paper, and Scissors Game using Python Programming

Random Password Generator using Python Programming

Shorten Your URL using Python and Bitly

Happy coding!

Hello, My Name is Rohit Kumar Thakur. I am open to freelancing. I build React Native projects and am currently working on Python Django. Feel free to contact me at (freelance.rohit7@gmail.com).




Continue Learning