Create a clock in python with explanation

To create a clock that shows the current time and updates every second in Python, you can use the tkinter library to create a GUI window.

This clock will display the current time in HH:MM:SS format, and we will use after() to update the time every second.

Code

import tkinter as tk
from time import strftime

# Create the main window
root = tk.Tk()
root.title("Digital Clock")

# Configure the time label
time_label = tk.Label(root, font=("Helvetica", 48), background="black", foreground="cyan")
time_label.pack(anchor="center")

# Define a function to update the time every second
def update_time():
    # Get the current time in HH:MM:SS format
    current_time = strftime("%H:%M:%S")
    
    # Update the label with the new time
    time_label.config(text=current_time)
    
    # Schedule the update_time function to run again after 1000 milliseconds (1 second)
    time_label.after(1000, update_time)

# Call the update_time function to start the clock
update_time()

# Run the main loop
root.mainloop()

Explanation of the Code

  1. Import Libraries:
    • tkinter is the standard Python GUI library, which we use to create the window and display the clock.
    • strftime from the time module helps format the current time as a string in the desired format.
  2. Set Up the Main Window:
    • root = tk.Tk(): Creates the main window for our application.
    • root.title(“Digital Clock”): Sets the title of the window to “Digital Clock”.
  3. Configure the Time Label:
    • time_label = tk.Label(…): Creates a Label widget that displays the time.
    • font=(“Helvetica”, 48): Sets the font to “Helvetica” with a size of 48.
    • background=”black”: Sets the background color of the label to black.
    • foreground=”cyan”: Sets the text color of the label to cyan.
    • time_label.pack(anchor=”center”): Places the label in the center of the window.
  4. Define the update_time Function:
    • current_time = strftime(“%H:%M:%S”): Gets the current time in HH:MM:SS format using strftime.
    • time_label.config(text=current_time): Updates the Label widget to show the current time.
    • time_label.after(1000, update_time): Calls the update_time function again after 1000 milliseconds (1 second) to refresh the time displayed.
  5. Start the Clock:
    • update_time(): Calls the update_time function for the first time, which initiates the time display and refresh cycle.
  6. Run the Main Loop:
    • root.mainloop(): Starts the tkinter main loop, which keeps the window open and responsive.

Explanation of the after() Method

The after() method is key to making this clock update every second. By calling after(1000, update_time), we tell the time_label to call update_time after 1000 milliseconds (1 second).

This creates a continuous loop, where update_time is called every second, updating the displayed time accordingly.

This clock will display the current time in real-time and update every second, giving the appearance of a working digital clock.

Related posts

BMI and BMR calculator using Tkinter

A Python program that generates a random password

a Python GUI-based random quote generator