moses-palmer / pystray

GNU General Public License v3.0
461 stars 57 forks source link

UPDATE ICONS TITLE #137

Closed knana1662 closed 1 year ago

knana1662 commented 1 year ago

Please how do I update an icons "title" every 5-10 seconds, if the icons".run" method is already called . Thank You @moses-palmer and others This is the code below:

`import sched import time from pystray import Menu as m,MenuItem as mi,Icon as i from PIL import Image from schedule import every import schedule from psutil import disk_usage,virtual_memory,cpu_count,cpu_percent from win11toast import toast,notify

img=Image.open(r"C:\Users\path\Downloads\Frame_1_rmbg.png") imge=Image.open(r"C:\Users\path\Downloads\Frame_1_rmbg.png")

seen=True _name="Background" _exit="Exit" title="Notify Background"

disk_path=disk_usage(path="C:/") disk_free=f"{disk_path.free / 1000000000}" # free space

usage=disk_free # 0.000 000000+ BYTES ,1.10GB,100MB,10MB, 1MB

print("=============================================")

print("usage",usage)

disk=f"{usage[:5]}"

print(disk)

_mem=virtual_memory() # Memory Usage mem=f"{_mem.percent}%" # Calculation

print(mem) # Result

_cpu= cpu_percent(interval=None) # cpu percentage cpu=f"{_cpu }%" # Calculation

print(cpu) # Result

_cpu_count=cpu_count(logical=True) # cpu count

print(_cpu_count) # Results

Normal="Normal" _title= f"Health: {Normal!r} \n Disk Usage: {disk}MB\n Memory Space: {mem} \n CPU Usage: {cpu} "

if disk>= f"{0.200}" and cpu or mem <= f"{65}%": print(f" Health : Normal \n Disk Usage: {disk}MB \n Memory Space: {mem} \n CPU Usage: {cpu} ")

else: print(f" Health : Abnormal \n Disk Usage: {disk[2:]}MB \n Memory Space: {mem} \n CPU Usage: {cpu} ")

TRAY ICON

icon=i(_name,img,_title, m( mi(_exit,exit) # Exits pystray ), ) icon._update_title()

icon.run()`

moses-palmer commented 1 year ago

Thank you for your report, and I apologise for this very late reply.

To update the icon title, simply modify its title attribute. To do this every five seconds, perhaps you could start a background thread or timer? How to do this is really out of scope for this library.