====== 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
icon.visible = False
python = sys.executable
os.execl(python, python, *sys.argv)
#os.execv(sys.executable, ['python'] + sys.argv)
def action_quit():
icon.visible = False
print("Exiting application")
os._exit(0)
def launch():
i = 0;
while i<1000000:
print(i)
i += 1
pass
def resource_path(relative_path):
try:
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
image = Image.open(resource_path("systrayicon.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_quit))
icon = icon("name", image, "title", menu)
def run_call(icon):
if icon is not None:
icon.visible = True
launch()
icon.run(run_call)
Create version file using:
pip install pyinstaller_versionfile
import pyinstaller_versionfile
pyinstaller_versionfile.create_versionfile(
output_file="version.txt",
version="1.2.3.4",
company_name="My Imaginary Company",
file_description="Simple App",
internal_name="Simple App",
legal_copyright="© My Imaginary Company. All rights reserved.",
original_filename="SimpleApp.exe",
product_name="Simple App"
)
To build as single file with additional data like images, the resource_path function needs to be included and pyinstaller needs to use the --add-data option. On Windows a semicolon is required, on linux a colon works.
wine pyinstaller.exe --onefile --icon=systrayicon.ico --add-data "systrayicon.png;." --version-file=version.txt systraytest.py
Pyinstaller options:
--onefile: Creates a single file. Synonyms: -F
--windowed: Ensures that no console is opened during execution. Synonyms: -w and --noconsole
--add-data: adds data to the file using "source:target" or "source;target"
--version: adds exe file version information
--icon: adds windows application icon file