I want the tray icon to change according to the value of p_out
. Specifically depending on its value, I want it to get a different color.
Here's the code
import pystray
import ping3
while True:
p_out = ping3.ping("google.com", unit="ms")
if p_out == 0:
img = white
elif p_out >= 999:
img = red
else:
print(f'\n{p_out:4.0f}', end='')
if p_out <= 50:
img = green
elif p_out <= 60:
img = yellow
elif p_out < 100:
img = orange
elif p_out >= 100:
img = red
icon = pystray.Icon(" ", img)
icon.run()
I tried to 'reset' the pystray icon
on every loop but it didn't work. The icon only changes when I stop and rerun the script.
As correctly stated in the comments, providing code that cannot run, does not help the community members to assist you. Τhe code references variables named
white
,red
,green
,yellow
andorange
, but these variables have not been defined or assigned values.Despite all this, the dynamic update of the tray icon is certainly something that can be useful to others. Therefore, below you may find your code with the necessary corrections applied.
NOTES:
pip install pillow
.