小编Dou*_*lva的帖子

使用 pystray 动态更改托盘图标

我希望托盘图标根据 的值进行更改p_out。具体取决于它的值,我希望它有不同的颜色。

这是代码

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()
Run Code Online (Sandbox Code Playgroud)

我尝试在每个循环上“重置”pystray,icon但它不起作用。仅当我停止并重新运行脚本时,图标才会发生变化。

python taskbar pystray

5
推荐指数
1
解决办法
1296
查看次数

标签 统计

pystray ×1

python ×1

taskbar ×1