User Tools

Site Tools


python:systray

This is an old revision of the document!


Py Systray

https://stackoverflow.com/questions/47095129/pystray-systray-icon

https://stackoverflow.com/questions/9494739/how-to-build-a-systemtray-app-for-windows

https://pystray.readthedocs.io/en/latest/index.html

https://github.com/moses-palmer/pystray

https://github.com/PySimpleGUI/PySimpleGUI/tree/master/PySimpleGUIWx
https://www.reddit.com/r/Python/comments/a9t466/system_tray_icon_get_status_launch_programs_popup/

Example:

pip install pystray pillow
#!/usr/bin/python
#import pystray
from pystray import Icon as icon, Menu as menu, MenuItem as item
from PIL import Image
import os, sys

def action():
    pass

def action_restart():
    """Restarts the current program, with file objects and descriptors
       cleanup
    """

    try:
        p = psutil.Process(os.getpid())
        for handler in p.get_open_files() + p.connections():
            os.close(handler.fd)
    except Exception as e:
        #logging.error(e)
        pass

    python = sys.executable
    os.execl(python, python, *sys.argv)
    #os.execv(sys.executable, ['python'] + sys.argv)

def action_exit():
    quit()

def launch():
    i = 0;
    while i<1000000:
        print(i)
        i += 1
    pass

image = Image.open("systray-icon.png")
#icon = pystray.Icon(name ="just text", icon =image, title ="Other Text", menu =None)
menu = (item('name', action), item('Restart', action_restart), item('Exit', action_exit))
icon = icon("name", image, "title", menu)

def run_call(icon):
    if icon is not None:
        icon.visible = True
    launch()

icon.run(run_call)
python/systray.1675005230.txt.gz · Last modified: 2023/05/29 11:53 (external edit)