-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpu_indicator.pyw
77 lines (56 loc) · 2.13 KB
/
cpu_indicator.pyw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import time
from threading import Thread
import pystray
from PIL import Image, ImageDraw, ImageFont
from psutil import cpu_percent
class Indicator:
# changeable variables
REFRESH_INTERVAL = 0.5
FONT_SIZE = 200
ICON_RES = 250, 250
FONT = ImageFont.truetype('arial.ttf', FONT_SIZE)
BLUE_BG = Image.new('RGB', ICON_RES, color=(61, 75, 129))
RED_BG = Image.new('RGB', ICON_RES, color=(255, 0, 0))
def __init__(self):
self.icon = pystray.Icon("usage", title="CPU Usage")
self.icon.menu = pystray.Menu(pystray.MenuItem('Exit', lambda: self.exit_action()))
self.refresh_icon_thread = None
# this is so that our icon refresher thread knows when to stop
self.stopped = False
def exit_action(self):
self.icon.stop()
self.stopped = True
def refresh_icon(self):
while True:
if self.stopped is True:
return
# ceil the usage without the math lib
# to decrease the size of the .exe file
# (this most likely didnt help at all)
usage = cpu_percent(interval=0)
usage = int(- (-usage // 1))
# decide bg depending on usage
image = self.RED_BG.copy() if usage > 70 else self.BLUE_BG.copy()
# draw the usage text over bg
draw = ImageDraw.Draw(image)
draw.text(
# center the usage text
xy=(
self.ICON_RES[0] / 2,
self.ICON_RES[1] / 2
),
anchor="mm", # https://pillow.readthedocs.io/en/stable/handbook/text-anchors.html#text-anchors
text=str(usage),
font=self.FONT,
fill=(255, 255, 255),
)
self.icon.icon = image
time.sleep(self.REFRESH_INTERVAL)
def start(self):
self.refresh_icon_thread = Thread(daemon=True, target=self.refresh_icon)
self.refresh_icon_thread.start()
# wait a bit so that an icon is set
time.sleep(0.5)
self.icon.run()
self.refresh_icon_thread.join()
Indicator().start()