@@ -101,18 +101,15 @@ def clean_stop(tray_icon=None):
101101
102102 # Remove tray icon just before exit
103103 if tray_icon :
104- tray_icon .visible = False
104+ tray_icon .stop ()
105+ time .sleep (0.5 )
105106
106107 # We force the exit to avoid waiting for other scheduled tasks: they may have a long delay!
107108 try :
108109 sys .exit (0 )
109110 except :
110111 os ._exit (0 )
111112
112- def on_signal_caught (signum , frame = None ):
113- logger .info ("Caught signal %d, exiting" % signum )
114- clean_stop ()
115-
116113 def on_configure_tray (tray_icon , item ):
117114 logger .info ("Configure from tray icon" )
118115
@@ -135,17 +132,46 @@ def on_exit_tray(tray_icon, item):
135132 clean_stop (tray_icon )
136133
137134
135+ # Create a tray icon for the program, with an Exit entry in menu
136+ try :
137+ tray_icon = pystray .Icon (
138+ name = 'Turing System Monitor' ,
139+ title = 'Turing System Monitor' ,
140+ icon = Image .open (MAIN_DIRECTORY / "res/icons/monitor-icon-17865/64.png" ),
141+ menu = pystray .Menu (
142+ pystray .MenuItem (
143+ text = 'Configure' ,
144+ action = on_configure_tray ),
145+ pystray .Menu .SEPARATOR ,
146+ pystray .MenuItem (
147+ text = 'Exit' ,
148+ action = on_exit_tray )
149+ )
150+ )
151+
152+ # For platforms != macOS, display the tray icon now with non-blocking function
153+ if platform .system () != "Darwin" :
154+ tray_icon .run_detached ()
155+ logger .info ("Tray icon has been displayed" )
156+ except :
157+ tray_icon = None
158+ logger .warning ("Tray icon is not supported on your platform" )
159+
160+ # Set the different stopping event handlers, to send a complete frame to the LCD before exit
138161 def on_clean_exit (* args ):
139162 logger .info ("Program will now exit" )
140- clean_stop ()
163+ clean_stop (tray_icon )
141164
165+ def on_signal_caught (signum , frame = None ):
166+ logger .info ("Caught signal %d, exiting" % signum )
167+ clean_stop (tray_icon )
142168
143169 if platform .system () == "Windows" :
144170 def on_win32_ctrl_event (event ):
145171 """Handle Windows console control events (like Ctrl-C)."""
146172 if event in (win32con .CTRL_C_EVENT , win32con .CTRL_BREAK_EVENT , win32con .CTRL_CLOSE_EVENT ):
147173 logger .debug ("Caught Windows control event %s, exiting" % event )
148- clean_stop ()
174+ clean_stop (tray_icon )
149175 return 0
150176
151177
@@ -166,34 +192,8 @@ def on_win32_wm_event(hWnd, msg, wParam, lParam):
166192 else :
167193 # For any other events, the program will stop
168194 logger .info ("Program will now exit" )
169- clean_stop ()
195+ clean_stop (tray_icon )
170196
171- # Create a tray icon for the program, with an Exit entry in menu
172- try :
173- tray_icon = pystray .Icon (
174- name = 'Turing System Monitor' ,
175- title = 'Turing System Monitor' ,
176- icon = Image .open (MAIN_DIRECTORY / "res/icons/monitor-icon-17865/64.png" ),
177- menu = pystray .Menu (
178- pystray .MenuItem (
179- text = 'Configure' ,
180- action = on_configure_tray ),
181- pystray .Menu .SEPARATOR ,
182- pystray .MenuItem (
183- text = 'Exit' ,
184- action = on_exit_tray )
185- )
186- )
187-
188- # For platforms != macOS, display the tray icon now with non-blocking function
189- if platform .system () != "Darwin" :
190- tray_icon .run_detached ()
191- logger .info ("Tray icon has been displayed" )
192- except :
193- tray_icon = None
194- logger .warning ("Tray icon is not supported on your platform" )
195-
196- # Set the different stopping event handlers, to send a complete frame to the LCD before exit
197197 atexit .register (on_clean_exit )
198198 signal .signal (signal .SIGINT , on_signal_caught )
199199 signal .signal (signal .SIGTERM , on_signal_caught )
0 commit comments