How To Make A Digital Clock In Python Using Tkinter

0
802

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 ReadHow 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()

Download the Python File.

Let’s now understand how this Python code works to create a colorful digital clock.

1Importing 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')

2Creating 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)

3Customizing 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')

4Placing 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.

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 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 codeitbro@gmail.com.

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 :)