1+ import tkinter as tk
2+ import time
3+ import math
4+
5+ class ClockApp :
6+ def __init__ (self ):
7+ self .root = tk .Tk ()
8+ self .root .title ("Analog Clock" )
9+
10+ self .canvas = tk .Canvas (self .root , width = 400 , height = 400 )
11+ self .canvas .pack ()
12+
13+ self .clock_face = self .canvas .create_oval (50 , 50 , 350 , 350 , width = 2 )
14+ self .hour_hand = self .canvas .create_line (200 , 200 , 200 , 200 , width = 4 )
15+ self .minute_hand = self .canvas .create_line (200 , 200 , 200 , 200 , width = 3 )
16+ self .second_hand = self .canvas .create_line (200 , 200 , 200 , 200 , width = 2 )
17+
18+ self .update_clock ()
19+
20+ def update_clock (self ):
21+ current_time = time .strftime ("%H:%M:%S" )
22+ hour , minute , second = current_time .split (":" )
23+
24+ hour_angle = (int (hour ) % 12 ) * 30 + int (minute ) / 2
25+ minute_angle = int (minute ) * 6 + int (second ) / 10
26+ second_angle = int (second ) * 6
27+
28+ self .canvas .delete ("all" )
29+ self .clock_face = self .canvas .create_oval (50 , 50 , 350 , 350 , width = 2 )
30+ self .hour_hand = self .canvas .create_line (200 , 200 , 200 + 80 * math .sin (math .radians (hour_angle )), 200 - 80 * math .cos (math .radians (hour_angle )), width = 4 )
31+ self .minute_hand = self .canvas .create_line (200 , 200 , 200 + 120 * math .sin (math .radians (minute_angle )), 200 - 120 * math .cos (math .radians (minute_angle )), width = 3 )
32+ self .second_hand = self .canvas .create_line (200 , 200 , 200 + 140 * math .sin (math .radians (second_angle )), 200 - 140 * math .cos (math .radians (second_angle )), width = 2 )
33+
34+ self .root .after (1000 , self .update_clock )
35+
36+ def run (self ):
37+ self .root .mainloop ()
38+
39+ if __name__ == "__main__" :
40+ app = ClockApp ()
41+ app .run ()
0 commit comments