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