Skip to content

Commit 4c9a760

Browse files
authored
Merge pull request #112 from mathoudebine/feature/111-add-a-tray-icon
Feature/111 add a tray icon
2 parents a91a41d + af13586 commit 4c9a760

File tree

11 files changed

+381
-4
lines changed

11 files changed

+381
-4
lines changed

config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ display:
4747
DISPLAY_REVERSE: false
4848

4949
# Display resolution in portrait orientation
50-
# Do not use this setting to rotate display! Use DISPLAY_REVERSE
50+
# Do not use this setting to rotate display! The selected theme handles landscape/portrait orientation
5151
DISPLAY_WIDTH: 320 # Do not change unless you have a good reason
5252
DISPLAY_HEIGHT: 480 # Do not change unless you have a good reason
5353

library/stats.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,14 @@
1919
if HW_SENSORS == "PYTHON":
2020
import library.sensors.sensors_python as sensors
2121
elif HW_SENSORS == "LHM":
22-
import library.sensors.sensors_librehardwaremonitor as sensors
22+
if platform.system() == 'Windows':
23+
import library.sensors.sensors_librehardwaremonitor as sensors
24+
else:
25+
logger.error("LibreHardwareMonitor integration is only available on Windows")
26+
try:
27+
sys.exit(0)
28+
except:
29+
os._exit(0)
2330
elif HW_SENSORS == "STUB":
2431
import library.sensors.sensors_stub as sensors
2532
elif HW_SENSORS == "AUTO":

main.py

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# https://github.com/mathoudebine/turing-smart-screen-python
44
import locale
55
import os
6+
import platform
67
import signal
78
import sys
89
import time
@@ -15,6 +16,13 @@
1516
except:
1617
os._exit(0)
1718

19+
from PIL import Image
20+
21+
try:
22+
import pystray
23+
except:
24+
pass
25+
1826
from library.log import logger
1927
import library.scheduler as scheduler
2028
from library.display import display
@@ -24,9 +32,8 @@
2432
# Apply system locale to this program
2533
locale.setlocale(locale.LC_ALL, '')
2634

27-
def sighandler(signum, frame):
28-
logger.info(" Caught signal %d, exiting" % signum)
2935

36+
def clean_stop(tray_icon=None):
3037
# Do not stop the program now in case data transmission was in progress
3138
# Instead, ask the scheduler to empty the action queue before stopping
3239
scheduler.STOPPING = True
@@ -41,13 +48,47 @@ def sighandler(signum, frame):
4148

4249
logger.debug("(%.1fs)" % (5 - wait_time))
4350

51+
# Remove tray icon just before exit
52+
if tray_icon:
53+
tray_icon.visible = False
54+
4455
# We force the exit to avoid waiting for other scheduled tasks: they may have a long delay!
4556
try:
4657
sys.exit(0)
4758
except:
4859
os._exit(0)
4960

5061

62+
def sighandler(signum, frame=None):
63+
logger.info("Caught signal %d, exiting" % signum)
64+
clean_stop()
65+
66+
67+
def on_exit_tray(tray_icon, item):
68+
logger.info("Exit from tray icon")
69+
clean_stop(tray_icon)
70+
71+
72+
# Create a tray icon for the program, with an Exit entry in menu
73+
try:
74+
tray_icon = pystray.Icon(
75+
name='Turing System Monitor',
76+
title='Turing System Monitor',
77+
icon=Image.open("res/icons/monitor-icon-17865/64.png"),
78+
menu=pystray.Menu(
79+
pystray.MenuItem(
80+
'Exit',
81+
on_exit_tray))
82+
)
83+
84+
# For platforms != macOS, display the tray icon now with non-blocking function
85+
if platform.system() != "Darwin":
86+
tray_icon.run_detached()
87+
logger.info("Tray icon has been displayed")
88+
except:
89+
tray_icon = None
90+
logger.warning("Tray icon is not supported on your platform")
91+
5192
# Set the signal handlers, to send a complete frame to the LCD before exit
5293
signal.signal(signal.SIGINT, sighandler)
5394
signal.signal(signal.SIGTERM, sighandler)
@@ -81,3 +122,7 @@ def sighandler(signum, frame):
81122
scheduler.NetStats()
82123
scheduler.DateStats()
83124
scheduler.QueueHandler()
125+
126+
if tray_icon and platform.system() == "Darwin":
127+
# For macOS: display the tray icon now with blocking function
128+
tray_icon.run()

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ pyserial~=3.5 # Serial linl to communicate with the display
44
PyYAML~=6.0 # For themes files
55
psutil~=5.9.4 # CPU / disk / network metrics
66
GPUtil~=1.4.0 # Nvidia GPU
7+
pystray~=0.19.4 # Tray icon (all OS)
78

89
# Following packages are for AMD GPU on Linux
910
# Note: you may need to manually install Cython first
2.61 KB
Loading
517 Bytes
Loading
752 Bytes
Loading
946 Bytes
Loading
1.36 KB
Loading
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Icon by <a href="https://freeicons.io/profile/730">Anu Rocks</a> on <a href="https://freeicons.io">freeicons.io</a>
2+
3+
https://freeicons.io/regular-life-icons/monitor-icon-17865
4+
5+
This work is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by/3.0/">Creative Commons Attribution 3.0 Unported License</a>.

0 commit comments

Comments
 (0)