SoFunction
Updated on 2024-11-20

Python draw clock sample code

Import the required package setup variables

from datetime import datetime
from  import *
import sys, math, pygame


def print_text(font, x, y, text, color=(255, 255, 255)):
    img_text = (text, True, color)
    (img_text, (x, y))


def wrap_angle(angle):
    return angle % 360


()
screen = .set_mode((600, 500))
.set_caption("Online Clock")

# Set the font
font1 = ("Founder's Bold Black Song Simplified.", 24)
orange = 220, 180, 0
white = 255, 255, 255
yellow = 255, 255, 0
pink = 255, 100, 100

pos_x = 300
pos_y = 250
radius = 250
angle = 360

Here draw a 600*500 screen, set the center position of the circle and its radius. Depending on the time of day, the radius is calculated, and the corresponding coordinates are calculated by trigonometry to draw the line of the hour hand from the position of the center of the circle.

write figures

Write the numbers in the corresponding positions by cycling through 1 through 12, -10 and the corresponding fonts are fine-tuned to make them more aligned

    for n in range(1, 13):
        angle = (n * (360 / 12) - 90)
        x = (angle) * (radius - 20) - 10
        y = (angle) * (radius - 20) - 10
        print_text(font1, pos_x + x, pos_y + y, str(n))

Drawing the hour hand

Here the corresponding angle is calculated from the time and then the hour hand is drawn from the center of the circle to the specified position.

    hour_angle = wrap_angle(hours * (360/12) - 90) + wrap_angle(minutes * (360/60) - 90) / 60
    hour_angle = (hour_angle)
    hour_x = (hour_angle) * (radius - 80)
    hour_y = (hour_angle) * (radius - 80)
    target = (pos_x + hour_x, pos_y + hour_y)
    (screen, pink, (pos_x, pos_y), target, 25)

Full Code

from datetime import datetime
from  import *
import sys, math, pygame


def print_text(font, x, y, text, color=(255, 255, 255)):
    img_text = (text, True, color)
    (img_text, (x, y))


def wrap_angle(angle):
    return angle % 360


()
screen = .set_mode((600, 500))
.set_caption("Online Clock")

# Set the font
font1 = ("Founder's Bold Black Song Simplified.", 24)
orange = 220, 180, 0
white = 255, 255, 255
yellow = 255, 255, 0
pink = 255, 100, 100

pos_x = 300
pos_y = 250
radius = 250
angle = 360

while True:
    for event in ():
        if  == QUIT:
            ()
            ()

    keys = .get_pressed()
    if keys[K_ESCAPE]:
        ()
        ()

    ((154, 205, 255))
    (screen, white, (pos_x, pos_y), radius, 6)

    for n in range(1, 13):
        angle = (n * (360 / 12) - 90)
        x = (angle) * (radius - 20) - 10
        y = (angle) * (radius - 20) - 10
        print_text(font1, pos_x + x, pos_y + y, str(n))

    today = ()
    hours =  % 12
    minutes = 
    seconds = 

    hour_angle = wrap_angle(hours * (360/12) - 90) + wrap_angle(minutes * (360/60) - 90) / 60
    hour_angle = (hour_angle)
    hour_x = (hour_angle) * (radius - 80)
    hour_y = (hour_angle) * (radius - 80)
    target = (pos_x + hour_x, pos_y + hour_y)
    (screen, pink, (pos_x, pos_y), target, 25)

    min_angle = wrap_angle(minutes * (360/60) - 90)
    min_angle = (min_angle)
    min_x = (min_angle) * (radius - 60)
    min_y = (min_angle) * (radius - 60)
    target = (pos_x + min_x, pos_y + min_y)
    (screen, orange, (pos_x, pos_y), target, 12)

    sec_angle = wrap_angle(seconds * (360/60) - 90)
    sec_angle = (sec_angle)
    sec_x = (sec_angle) * (radius - 40)
    sec_y = (sec_angle) * (radius - 40)
    target = (pos_x + sec_x, pos_y + sec_y)
    (screen, yellow, (pos_x, pos_y), target, 6)

    (screen, white, (pos_x, pos_y), 20)
    print_text(font1, 0, 0, str(hours) + ":" + str(minutes) + ":" + str(seconds))

    ()

Above is Python draw clock sample code details, more information about Python draw clock please pay attention to my other related articles!