33# https://github.com/mathoudebine/turing-smart-screen-python
44import locale
55import os
6+ import pystray
67import signal
78import sys
89import time
910
11+ from PIL import Image
12+
1013MIN_PYTHON = (3 , 7 )
1114if sys .version_info < MIN_PYTHON :
1215 print ("[ERROR] Python %s.%s or later is required." % MIN_PYTHON )
2427 # Apply system locale to this program
2528 locale .setlocale (locale .LC_ALL , '' )
2629
27- def sighandler (signum , frame ):
28- logger .info (" Caught signal %d, exiting" % signum )
2930
31+ def clean_stop (tray_icon = None ):
3032 # Do not stop the program now in case data transmission was in progress
3133 # Instead, ask the scheduler to empty the action queue before stopping
3234 scheduler .STOPPING = True
@@ -41,13 +43,27 @@ def sighandler(signum, frame):
4143
4244 logger .debug ("(%.1fs)" % (5 - wait_time ))
4345
46+ # Remove icon just before the exit
47+ if tray_icon :
48+ tray_icon .visible = False
49+
4450 # We force the exit to avoid waiting for other scheduled tasks: they may have a long delay!
4551 try :
4652 sys .exit (0 )
4753 except :
4854 os ._exit (0 )
4955
5056
57+ def sighandler (signum , frame = None ):
58+ logger .info ("Caught signal %d, exiting" % signum )
59+ clean_stop ()
60+
61+
62+ def on_exit_tray (tray_icon , item ):
63+ logger .info ("Exit from tray icon" )
64+ clean_stop (tray_icon )
65+
66+
5167 # Set the signal handlers, to send a complete frame to the LCD before exit
5268 signal .signal (signal .SIGINT , sighandler )
5369 signal .signal (signal .SIGTERM , sighandler )
@@ -81,3 +97,14 @@ def sighandler(signum, frame):
8197 scheduler .NetStats ()
8298 scheduler .DateStats ()
8399 scheduler .QueueHandler ()
100+
101+ # Create a tray icon for the program, with an Exit entry in menu
102+ tray_icon = pystray .Icon (
103+ name = 'Turing System Monitor' ,
104+ title = 'Turing System Monitor' ,
105+ icon = Image .open ("res/icons/724873501557740364-64.png" ),
106+ menu = pystray .Menu (
107+ pystray .MenuItem (
108+ 'Exit' ,
109+ on_exit_tray ))
110+ ).run ()
0 commit comments