Analog clock python code
import turtle
import time
# Set up the screen
wn = turtle.Screen()
wn.bgcolor("white")
wn.setup(width=600, height=600)
wn.title("Analog Clock")
wn.tracer(0)
# Create the drawing pen
pen = turtle.Turtle()
pen.hideturtle()
pen.speed(0)
pen.pensize(3)
# Function to draw the clock face
def draw_clock(h, m, s, pen):
# Draw the clock face
pen.up()
pen.goto(0, 210)
pen.setheading(180)
pen.color("black")
pen.pendown()
pen.circle(210)
# Draw the hour lines
pen.up()
pen.goto(0, 0)
pen.setheading(90)
for _ in range(12):
pen.fd(190)
pen.pendown()
pen.fd(20)
pen.up()
pen.goto(0, 0)
pen.rt(30)
# Draw the hour hand
pen.up()
pen.goto(0, 0)
pen.color("black")
pen.setheading(90)
angle = (h / 12) * 360
pen.rt(angle)
pen.pendown()
pen.fd(100)
# Draw the minute hand
pen.up()
pen.goto(0, 0)
pen.color("blue")
pen.setheading(90)
angle = (m / 60) * 360
pen.rt(angle)
pen.pendown()
pen.fd(150)
# Draw the second hand
pen.up()
pen.goto(0, 0)
pen.color("red")
pen.setheading(90)
angle = (s / 60) * 360
pen.rt(angle)
pen.pendown()
pen.fd(180)
# Main loop
while True:
# Get the current time
h = int(time.strftime("%I"))
m = int(time.strftime("%M"))
s = int(time.strftime("%S"))
# Draw the clock
pen.clear()
draw_clock(h, m, s, pen)
wn.update()
# Wait a second
time.sleep(1)
wn.mainloop()
Comments