简体   繁体   中英

Dynamically change tray icon with pystray

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.

  1. Τhe code references variables named white , red , green , yellow and orange , but these variables have not been defined or assigned values.
  2. The if statements are conflicting with each other. This means that the different if statements in the provided code are not correctly handling all possible values of p_out , and as a result, the correct image may not be assigned to the img variable.

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.

import ping3
import pystray
import threading  # Import the threading module for creating threads
from PIL import Image  # Import the Image module from PIL for creating images


def update_icon():
    while True:
        ping_output = ping3.ping("google.com", unit="ms")
        print(f'\n{ping_output:4.0f}', end='')
        if ping_output == 0:
            img = Image.new("RGB", (32, 32), (255, 255, 255))  # 32px32px, white
        elif 0 < ping_output <= 50:
            img = Image.new("RGB", (32, 32), (0, 255, 0))  # 32px32px, green
        elif 50 < ping_output <= 60:
            img = Image.new("RGB", (32, 32), (255, 255, 0))  # 32px32px, yellow
        elif 60 < ping_output < 100:
            img = Image.new("RGB", (32, 32), (255, 165, 0))  # 32px32px, orange
        elif ping_output >= 100:
            img = Image.new("RGB", (32, 32), (255, 0, 0))  # 32px32px, red
        icon.icon = img


if __name__ == "__main__":
    icon = pystray.Icon("ping")
    icon.icon = Image.new("RGB", (32, 32), (255, 255, 255))  # 32px32px, white
    # Create a new thread to run the update_icon() function
    thread = threading.Thread(target=update_icon)
    # Start the thread
    thread.start()
    icon.run()

NOTES:

  • In order to use the Image module , you must first install Pillow , with a package manager like pip , using a command like this: pip install pillow .
  • The purpose of creating a new thread -to run update_icon function- is to allow the tray icon to continue updating in the background, without blocking the main thread of execution.
  • While 32x32px is a common size for icons, it is not necessarily the only size that can be used.
  • Using a while True loop to run a continuous task can consume a large amount of system resources, such as CPU and memory, which can impact the overall performance of your program. However, I will not suggest anything about it as it is not relevant to the present question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM