“How to Make an Analog Clock using Python tkinter GUI Toolkit” #coding #python #shorts

Posted by

Creating an Analog Clock in Python tkinter GUI Toolkit

Create Analog Clock in Python tkinter GUI Toolkit

In this article, we will learn how to create an analog clock using Python’s tkinter GUI toolkit. Tkinter is a popular library for creating graphical user interfaces in Python, and it provides a simple way to create windows, dialogs, and various widgets, including a canvas widget that we will use to draw the analog clock.

Setting Up the Environment

First, make sure you have Python installed on your computer. You can download and install Python from the official website.

Creating the Analog Clock

Now, let’s start by creating the code for our analog clock. We will use the tkinter library to create the window and canvas, and then draw the clock hands and numbers using the canvas widget.

import tkinter as tk
import time
import math

def create_analog_clock():
    root = tk.Tk()
    root.title("Analog Clock")

    canvas = tk.Canvas(root, width=400, height=400)
    canvas.pack()

    def draw_clock():
        canvas.delete("all")
        radius = 150
        center_x, center_y = 200, 200
        canvas.create_oval(center_x - radius, center_y - radius, center_x + radius, center_y + radius)

        current_time = time.localtime()
        hour = current_time.tm_hour % 12
        minute = current_time.tm_min
        second = current_time.tm_sec

        hour_angle = math.radians((hour * 30) + (minute * 0.5))
        minute_angle = math.radians((minute * 6) + (second * 0.1))
        second_angle = math.radians(second * 6)

        hour_hand_x = center_x + (70 * math.sin(hour_angle))
        hour_hand_y = center_y - (70 * math.cos(hour_angle))
        canvas.create_line(center_x, center_y, hour_hand_x, hour_hand_y, width=8, fill="black")

        minute_hand_x = center_x + (100 * math.sin(minute_angle))
        minute_hand_y = center_y - (100 * math.cos(minute_angle))
        canvas.create_line(center_x, center_y, minute_hand_x, minute_hand_y, width=4, fill="black")

        second_hand_x = center_x + (120 * math.sin(second_angle))
        second_hand_y = center_y - (120 * math.cos(second_angle))
        canvas.create_line(center_x, center_y, second_hand_x, second_hand_y, width=2, fill="red")

        root.after(1000, draw_clock)

    draw_clock()

    root.mainloop()

create_analog_clock()
    

This code creates a window and a canvas widget using tkinter, and then defines a function to draw the analog clock hands and numbers on the canvas. The draw_clock function uses the current time to calculate the angles for the hour, minute, and second hands, and then draws them on the canvas. The root.after method is used to update the clock every second, creating a ticking effect.

Conclusion

Creating an analog clock using Python’s tkinter GUI toolkit is a fun and educational exercise, and it demonstrates the power and flexibility of Python for creating graphical user interfaces. You can further customize the appearance and behavior of the analog clock by modifying the code, and even add additional features such as alarm functionality or interactive settings.