Digital Clock using Tkinter Python

Introduction

This project is a simple digital clock application built using Python’s Tkinter library. The clock displays the current time in hours, minutes, and seconds, updating every second. This project serves as an introduction to GUI programming in Python and provides a basic understanding of how to create and manipulate graphical elements.

Prerequisites

  • Basic understanding of Python programming.
  • Familiarity with the Tkinter library for building GUI applications.

Learning Objectives

  • By completing this project, you will learn how to:

    • Use the Tkinter library to create a graphical user interface (GUI) in Python.
    • Update the GUI elements dynamically in real-time.
    • Understand the basics of using functions and the after() method in Tkinter for repetitive tasks.

Installation Guide

To run this project on your machine, ensure you have Python installed. Follow these steps:

  1. Download and Install Python: Visit the official Python website to download and install Python if you haven’t already.
  2. Set Up a Python Environment: Use a code editor like Visual Studio Code or an IDE like PyCharm to write and run your code.
  3. Clone or Copy the Code: Copy the project code provided below into a new Python file in your development environment.

Project Steps

  1. Create a Python file: Open a text editor or an IDE (like VS Code, PyCharm, or IDLE) and create a new Python file named digital_clock.py.
  2. Import Tkinter and other modules: Start by importing the necessary libraries.
  3. Create the main application window: Use Tkinter to create the main window where the clock will be displayed.
  4. Design the clock display: Define the label to display the time, setting its font, background, and other styling options.
  5. Define the clock update function: Create a function that updates the time every second.
  6. Run the application: Start the Tkinter main event loop to display the window and run the application.

Code

				
					from tkinter import Label, Tk 
import time
app_window = Tk() 
app_window.title("Digital Clock") 
app_window.geometry("420x150") 
app_window.resizable(1,1)

text_font= ("Boulder", 68, 'bold')
background = "#f2e750"
foreground= "#363529"
border_width = 25

label = Label(app_window, font=text_font, bg=background, fg=foreground, bd=border_width) 
label.grid(row=0, column=1)

def digital_clock(): 
    time_live = time.strftime("%H:%M:%S")
    label.config(text=time_live) 
    label.after(200, digital_clock)

digital_clock()
app_window.mainloop()
				
			

Code

				
					from tkinter import Label, Tk 
import time
app_window = Tk() 
app_window.title("Digital Clock") 
app_window.geometry("420x150") 
app_window.resizable(1,1)

text_font= ("Boulder", 68, 'bold')
background = "#f2e750"
foreground= "#363529"
border_width = 25

label = Label(app_window, font=text_font, bg=background, fg=foreground, bd=border_width) 
label.grid(row=0, column=1)

def digital_clock(): 
    time_live = time.strftime("%H:%M:%S")
    label.config(text=time_live) 
    label.after(200, digital_clock)

digital_clock()
app_window.mainloop()
				
			

Pseudo Code explaining this Python Project

				
					1.	Import necessary libraries.
2.	Create the main application window.
3.	Set window properties (title, size, resizability).
4.	Define the label to display the time with desired font and style.
5.	Create a function to get the current time and update the label text.
6.	Use the after() method to repeatedly call the update function.
7.	Start the Tkinter main event loop.

				
			

Pseudo Code explaining this Python Project

				
					1.	Import necessary libraries.
2.	Create the main application window.
3.	Set window properties (title, size, resizability).
4.	Define the label to display the time with desired font and style.
5.	Create a function to get the current time and update the label text.
6.	Use the after() method to repeatedly call the update function.
7.	Start the Tkinter main event loop.

				
			

Flow Chart

Code Explanation

  • Import Statements: Label and Tk are imported from Tkinter, and time is imported for fetching the current time.
  • Tk(): Initializes the main application window.
  • Label: A Tkinter widget used to display text or images. It is configured here to display the time.
  • grid(): Places the label on the grid layout of the window.
  • digital_clock(): A function that gets the current system time and updates the label every 200 milliseconds using the after() method.
  • mainloop(): Starts the Tkinter event loop, which waits for events such as button clicks or updates to the display.

Challenges and Solutions

  • Challenge: Understanding how the after() method works for repeated tasks.
    • Solution: The after() method schedules a function to be called after a given amount of time. Here, it is used to call digital_clock() every 200 milliseconds, creating a loop to update the time.
  • Challenge: Designing the GUI layout and styling.
    • Solution: Experiment with different font sizes, colors, and window dimensions until the desired look is achieved.

Testing

  • Functionality: Ensure the clock updates every second.
  • Resizability: Test resizing the window to see if the clock adjusts correctly.
  • Appearance: Verify that the clock’s appearance matches the desired design specifications.

Additional Resources

FAQs

  1. Q1: Can I change the time format to 12-hour?

    Yes, modify the time_live line in the digital_clock function as follows:

    time_live = time.strftime(“%I:%M:%S %p”)

    Q2: How can I customize the font and colors?

    You can change the text_font, background, and foreground variables to your preferred values.

    Q3: What if the clock does not update?

    Ensure the after() method is correctly set to call the digital_clock function repeatedly. Double-check the time interval is appropriate (200 milliseconds for a smooth update).

Project by Nimisha Agrawal and Documented by Aakarsh Pandey, Team edSlash.