1+ import gi
2+ gi .require_version ("Gtk" , "3.0" )
3+ import math
4+ from datetime import datetime
5+ from gi .repository import Gtk , GLib , Pango
6+ import cairo
7+
8+
9+ class AnalogClock (Gtk .Window ):
10+ def __init__ (self ):
11+ super ().__init__ (title = "Analog Clock" )
12+ self .set_default_size (100 , 100 )
13+ self .set_resizable (False )
14+ self .set_skip_taskbar_hint (True )
15+ self .set_decorated (False )
16+ self .set_keep_below (True )
17+
18+
19+ self .connect ("destroy" , Gtk .main_quit )
20+
21+
22+ self .drawing_area = Gtk .DrawingArea ()
23+ self .drawing_area .connect ("draw" , self .on_draw )
24+ self .add (self .drawing_area )
25+
26+
27+ # Update the clock every second
28+ GLib .timeout_add (1000 , self .update_clock )
29+ self .menu = Gtk .Menu ()
30+
31+ close_item = Gtk .MenuItem (label = "close" )
32+ close_item .connect ("activate" , Gtk .main_quit )
33+ self .menu .append (close_item )
34+
35+ self .menu .show_all ()
36+
37+ self .connect ("button-press-event" , self .open_menu )
38+
39+ def update_clock (self ):
40+ self .drawing_area .queue_draw ()
41+ return True
42+
43+
44+ def on_draw (self , widget , cr ):
45+ width = widget .get_allocated_width ()
46+ height = widget .get_allocated_height ()
47+ radius = min (width , height ) / 2 - 10
48+
49+
50+ # Center of the clock
51+ center_x = width / 2
52+ center_y = height / 2
53+
54+
55+ # Get current time
56+ now = datetime .now ()
57+ seconds = now .second
58+ minutes = now .minute + seconds / 60.0
59+ hours = now .hour % 12 + minutes / 60.0
60+
61+
62+ # Draw clock face
63+ cr .set_source_rgb (0.9 , 0.9 , 0.9 ) # White background
64+ cr .paint ()
65+
66+
67+ # Draw clock border
68+ cr .set_source_rgb (0.20 , 0.20 , 0.20 ) # Black color
69+ cr .set_line_width (10 )
70+ cr .arc (center_x , center_y , radius , 0 , 2 * math .pi )
71+ cr .stroke ()
72+
73+
74+ # Draw hour hand
75+ hour_angle = (hours / 12 ) * 2 * math .pi - math .pi / 2
76+ hour_length = radius * 0.5
77+ cr .set_line_width (8 )
78+ cr .move_to (center_x , center_y )
79+ cr .line_to (center_x + hour_length * math .cos (hour_angle ), center_y + hour_length * math .sin (hour_angle ))
80+ cr .stroke ()
81+
82+
83+ # Draw minute hand
84+ minute_angle = (minutes / 60 ) * 2 * math .pi - math .pi / 2
85+ minute_length = radius * 0.75
86+ cr .set_line_width (5 )
87+ cr .move_to (center_x , center_y )
88+ cr .line_to (center_x + minute_length * math .cos (minute_angle ), center_y + minute_length * math .sin (minute_angle ))
89+ cr .stroke ()
90+
91+
92+ # Draw second hand
93+ second_angle = (seconds / 60 ) * 2 * math .pi - math .pi / 2
94+ second_length = radius * 0.9
95+ cr .set_source_rgb (1 , 0 , 0 ) # Red color for seconds
96+ cr .set_line_width (2 )
97+ cr .move_to (center_x , center_y )
98+ cr .line_to (center_x + second_length * math .cos (second_angle ), center_y + second_length * math .sin (second_angle ))
99+ cr .stroke ()
100+
101+
102+ # Draw tick marks and numbers
103+ cr .set_source_rgb (0 , 0 , 0 )
104+ for i in range (12 ):
105+ angle = (i / 12 ) * 2 * math .pi - math .pi / 0.17135555555
106+ start_x = center_x + (radius - 5 ) * math .cos (angle )
107+ start_y = center_y + (radius - 5 ) * math .sin (angle )
108+ end_x = center_x + (radius - 10 ) * math .cos (angle )
109+ end_y = center_y + (radius - 10 ) * math .sin (angle )
110+ cr .set_line_width (2 )
111+ cr .move_to (start_x , start_y )
112+ cr .line_to (end_x , end_y )
113+ cr .stroke ()
114+
115+
116+ # Draw the numbers
117+ cr .save ()
118+ cr .translate (center_x , center_y )
119+ cr .rotate (angle ) # Rotate to the correct position
120+ cr .move_to (- 6 , - (radius - 30 )) # Position the number
121+ cr .set_source_rgb (0 , 0 , 0 ) # Black color for numbers
122+ cr .select_font_face ("Sans" , cairo .FONT_SLANT_NORMAL , cairo .FONT_WEIGHT_BOLD )
123+ cr .set_font_size (13 )
124+ cr .show_text (str (i + 1 )) # Draw the number
125+ cr .restore ()
126+
127+ def open_menu (self , w , event ):
128+ if event .button == 3 : # Right-click
129+ self .menu .popup_at_pointer (event )
130+
131+
132+ if __name__ == "__main__" :
133+ clock = AnalogClock ()
134+ clock .show_all ()
135+ Gtk .main ()
0 commit comments