|
| 1 | +""" |
| 2 | +Analog Wall Clock using Python Turtle and Threading |
| 3 | +Issue #107 for king04aman/All-In-One-Python-Projects |
| 4 | +""" |
| 5 | +import turtle |
| 6 | +import threading |
| 7 | +import time |
| 8 | +from datetime import datetime |
| 9 | + |
| 10 | +WIDTH, HEIGHT = 600, 600 |
| 11 | + |
| 12 | +# Setup screen |
| 13 | +def setup_screen(): |
| 14 | + screen = turtle.Screen() |
| 15 | + screen.title("Analog Wall Clock") |
| 16 | + screen.bgcolor("white") |
| 17 | + screen.setup(width=WIDTH, height=HEIGHT) |
| 18 | + screen.tracer(0) |
| 19 | + return screen |
| 20 | + |
| 21 | +# Draw clock face |
| 22 | +def draw_clock_face(clock_turtle): |
| 23 | + clock_turtle.penup() |
| 24 | + clock_turtle.goto(0, -250) |
| 25 | + clock_turtle.pendown() |
| 26 | + clock_turtle.pensize(5) |
| 27 | + clock_turtle.color("black") |
| 28 | + clock_turtle.circle(250) |
| 29 | + clock_turtle.penup() |
| 30 | + clock_turtle.goto(0, 0) |
| 31 | + clock_turtle.pendown() |
| 32 | + # Draw numbers |
| 33 | + for i in range(1, 13): |
| 34 | + angle = i * 30 |
| 35 | + x = 200 * turtle.sin(turtle.radians(angle)) |
| 36 | + y = 200 * turtle.cos(turtle.radians(angle)) |
| 37 | + clock_turtle.penup() |
| 38 | + clock_turtle.goto(x, y-20) |
| 39 | + clock_turtle.pendown() |
| 40 | + clock_turtle.write(str(i), align="center", font=("Arial", 18, "bold")) |
| 41 | + # Decorative circles |
| 42 | + clock_turtle.penup() |
| 43 | + clock_turtle.goto(0, -220) |
| 44 | + clock_turtle.pendown() |
| 45 | + clock_turtle.pensize(2) |
| 46 | + clock_turtle.color("gray") |
| 47 | + clock_turtle.circle(220) |
| 48 | + clock_turtle.penup() |
| 49 | + clock_turtle.goto(0, -180) |
| 50 | + clock_turtle.pendown() |
| 51 | + clock_turtle.circle(180) |
| 52 | + clock_turtle.penup() |
| 53 | + clock_turtle.goto(0, 0) |
| 54 | + clock_turtle.pendown() |
| 55 | + |
| 56 | +# Draw AM/PM indicator |
| 57 | +def draw_ampm_indicator(clock_turtle): |
| 58 | + now = datetime.now() |
| 59 | + ampm = "AM" if now.hour < 12 else "PM" |
| 60 | + clock_turtle.penup() |
| 61 | + clock_turtle.goto(0, 120) |
| 62 | + clock_turtle.pendown() |
| 63 | + clock_turtle.color("blue") |
| 64 | + clock_turtle.write(ampm, align="center", font=("Arial", 16, "italic")) |
| 65 | + clock_turtle.penup() |
| 66 | + clock_turtle.goto(0, 0) |
| 67 | + clock_turtle.pendown() |
| 68 | + |
| 69 | +# Hand drawing functions |
| 70 | +def draw_hand(hand_turtle, length, angle, color, width): |
| 71 | + hand_turtle.clear() |
| 72 | + hand_turtle.penup() |
| 73 | + hand_turtle.goto(0, 0) |
| 74 | + hand_turtle.setheading(90) |
| 75 | + hand_turtle.right(angle) |
| 76 | + hand_turtle.pendown() |
| 77 | + hand_turtle.pensize(width) |
| 78 | + hand_turtle.color(color) |
| 79 | + hand_turtle.forward(length) |
| 80 | + hand_turtle.penup() |
| 81 | + hand_turtle.goto(0, 0) |
| 82 | + hand_turtle.pendown() |
| 83 | + |
| 84 | +# Thread functions |
| 85 | +def update_second_hand(hand_turtle): |
| 86 | + while True: |
| 87 | + now = datetime.now() |
| 88 | + angle = now.second * 6 |
| 89 | + draw_hand(hand_turtle, 180, angle, "red", 2) |
| 90 | + time.sleep(0.1) |
| 91 | + |
| 92 | +def update_minute_hand(hand_turtle): |
| 93 | + while True: |
| 94 | + now = datetime.now() |
| 95 | + angle = now.minute * 6 + now.second * 0.1 |
| 96 | + draw_hand(hand_turtle, 150, angle, "black", 4) |
| 97 | + time.sleep(0.5) |
| 98 | + |
| 99 | +def update_hour_hand(hand_turtle): |
| 100 | + while True: |
| 101 | + now = datetime.now() |
| 102 | + angle = (now.hour % 12) * 30 + now.minute * 0.5 |
| 103 | + draw_hand(hand_turtle, 100, angle, "black", 6) |
| 104 | + time.sleep(1) |
| 105 | + |
| 106 | +def main(): |
| 107 | + screen = setup_screen() |
| 108 | + clock_turtle = turtle.Turtle() |
| 109 | + clock_turtle.hideturtle() |
| 110 | + clock_turtle.speed(0) |
| 111 | + draw_clock_face(clock_turtle) |
| 112 | + draw_ampm_indicator(clock_turtle) |
| 113 | + |
| 114 | + # Create turtles for hands |
| 115 | + sec_turtle = turtle.Turtle() |
| 116 | + sec_turtle.hideturtle() |
| 117 | + sec_turtle.speed(0) |
| 118 | + min_turtle = turtle.Turtle() |
| 119 | + min_turtle.hideturtle() |
| 120 | + min_turtle.speed(0) |
| 121 | + hour_turtle = turtle.Turtle() |
| 122 | + hour_turtle.hideturtle() |
| 123 | + hour_turtle.speed(0) |
| 124 | + |
| 125 | + # Start threads |
| 126 | + threading.Thread(target=update_second_hand, args=(sec_turtle,), daemon=True).start() |
| 127 | + threading.Thread(target=update_minute_hand, args=(min_turtle,), daemon=True).start() |
| 128 | + threading.Thread(target=update_hour_hand, args=(hour_turtle,), daemon=True).start() |
| 129 | + |
| 130 | + # Update AM/PM indicator every minute |
| 131 | + def update_ampm(): |
| 132 | + while True: |
| 133 | + clock_turtle.clear() |
| 134 | + draw_clock_face(clock_turtle) |
| 135 | + draw_ampm_indicator(clock_turtle) |
| 136 | + time.sleep(60) |
| 137 | + threading.Thread(target=update_ampm, daemon=True).start() |
| 138 | + |
| 139 | + while True: |
| 140 | + screen.update() |
| 141 | + time.sleep(0.05) |
| 142 | + |
| 143 | +if __name__ == "__main__": |
| 144 | + main() |
0 commit comments