Skip to content

Commit 21ae4b7

Browse files
committed
Add Analog Wall Clock project (Issue #107)
1 parent 1a5bb98 commit 21ae4b7

File tree

2 files changed

+175
-0
lines changed

2 files changed

+175
-0
lines changed

Analog_Wall_Clock/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Analog Wall Clock
2+
3+
A real-time analog wall clock built using Python’s turtle graphics and threading.
4+
5+
## Features
6+
- Hour, minute, and second hands update in real time
7+
- Decorative clock face with numbers and circles
8+
- AM/PM indicator
9+
- Hands rotate in sync with system time
10+
11+
## Technologies Used
12+
- Python
13+
- turtle (graphics)
14+
- threading (concurrent hand updates)
15+
- datetime (system time)
16+
17+
## Usage
18+
1. Run `clock.py`.
19+
2. The clock window will appear and update in real time.
20+
21+
## Requirements
22+
- Python 3.x
23+
- turtle (usually included with Python)
24+
25+
## Extensibility
26+
- Add digital clock overlay
27+
- Custom themes (dark/light mode)
28+
- Alarm feature or tick sounds
29+
30+
## License
31+
MIT

Analog_Wall_Clock/clock.py

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
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

Comments
 (0)