pystray is a Python library that provides cross-platform system tray icon integration. It allows developers to create a system tray icon, define a title, set an image, and attach a popup menu with various actions. The current stable version is 0.19.5, with releases typically occurring as needed for bug fixes and feature enhancements, rather than on a strict schedule.
pip install pystray pillowVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to create a basic system tray icon with a custom image and a context menu. It defines functions for menu actions and runs the icon in a blocking loop. For integration with other GUI frameworks, `run_detached()` or threading should be considered.
For macOS, always call `icon.run()` from the main thread. If integrating with another GUI framework that requires the main thread, use `icon.run_detached()` in a separate thread.
Check `pystray.Icon.HAS_MENU` property at runtime to determine if menu functionality is available on the current platform and backend. Consider providing alternative interactions for platforms without full menu support.
Ensure both `pystray` and `Pillow` are updated to their latest stable versions (`pip install --upgrade pystray pillow`). If encountering issues with very new Python versions (e.g., 3.13+), consider using a slightly older Python version (e.g., 3.12) until full compatibility is established.
Use `icon.run_detached()` in a separate thread to allow both the tray icon and the GUI framework to run concurrently.