Skip to content

Commit 21f554f

Browse files
authored
Code
1 parent f73395b commit 21f554f

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

Code

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import tkinter as tk
2+
import time
3+
import math
4+
5+
class AdvancedAnalogClock(tk.Tk):
6+
def __init__(self):
7+
super().__init__()
8+
self.title("Advanced Analog Clock")
9+
self.geometry("400x400")
10+
self.canvas = tk.Canvas(self, bg='white', height=400, width=400)
11+
self.canvas.pack(fill='both', expand=True)
12+
self.update_clock()
13+
14+
def update_clock(self):
15+
self.canvas.delete("all")
16+
width = self.canvas.winfo_width()
17+
height = self.canvas.winfo_height()
18+
size = min(width, height) - 20
19+
center_x = width // 2
20+
center_y = height // 2
21+
current_time = time.localtime()
22+
hours = current_time.tm_hour
23+
minutes = current_time.tm_min
24+
seconds = current_time.tm_sec
25+
26+
# Draw clock face
27+
self.canvas.create_oval(center_x - size // 2, center_y - size // 2, center_x + size // 2, center_y + size // 2, fill='light gray')
28+
for i in range(1, 13):
29+
angle = math.radians(i * (360 / 12) - 90)
30+
x = center_x + (size // 2 - 20) * math.cos(angle)
31+
y = center_y + (size // 2 - 20) * math.sin(angle)
32+
self.canvas.create_text(x, y, text=str(i), font=('Times New Roman', 20, 'bold'))
33+
34+
# Draw tick marks
35+
for i in range(60):
36+
angle = math.radians(i * (360 / 60) - 90)
37+
start_x = center_x + (size // 2 - 10) * math.cos(angle)
38+
start_y = center_y + (size // 2 - 10) * math.sin(angle)
39+
end_x = center_x + (size // 2) * math.cos(angle)
40+
end_y = center_y + (size // 2) * math.sin(angle)
41+
self.canvas.create_line(start_x, start_y, end_x, end_y, width=1 if i % 5 else 2)
42+
43+
# Draw hour hand
44+
hour_angle = math.radians((hours % 12 + minutes / 60) * (360 / 12) - 90)
45+
hour_x = center_x + (size // 4) * math.cos(hour_angle)
46+
hour_y = center_y + (size // 4) * math.sin(hour_angle)
47+
self.canvas.create_line(center_x, center_y, hour_x, hour_y, width=6, fill='black')
48+
49+
# Draw minute hand
50+
minute_angle = math.radians(minutes * (360 / 60) - 90)
51+
minute_x = center_x + (size // 3) * math.cos(minute_angle)
52+
minute_y = center_y + (size // 3) * math.sin(minute_angle)
53+
self.canvas.create_line(center_x, center_y, minute_x, minute_y, width=4, fill='blue')
54+
55+
# Draw second hand
56+
second_angle = math.radians(seconds * (360 / 60) - 90)
57+
second_x = center_x + (size // 2 - 20) * math.cos(second_angle)
58+
second_y = center_y + (size // 2 - 20) * math.sin(second_angle)
59+
self.canvas.create_line(center_x, center_y, second_x, second_y, fill='red')
60+
61+
# Schedule the function to be called after 1000ms
62+
self.after(1000, self.update_clock)
63+
64+
if __name__ == "__main__":
65+
app = AdvancedAnalogClock()
66+
app.mainloop()

0 commit comments

Comments
 (0)