pystray

0.19.5 · active · verified Thu Apr 16

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.

Common errors

Warnings

Install

Imports

Quickstart

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.

import pystray
from PIL import Image, ImageDraw
import os

def create_image(width, height, color1, color2):
    # Generate a simple image for the icon
    image = Image.new('RGB', (width, height), color1)
    dc = ImageDraw.Draw(image)
    dc.rectangle((width // 2, 0, width, height // 2), fill=color2)
    dc.rectangle((0, height // 2, width // 2, height), fill=color2)
    return image

def quit_action(icon, item):
    icon.stop()

def show_message(icon, item):
    print("Hello from the tray icon!")

# Create an image for the icon
icon_image = create_image(64, 64, 'black', 'white')

# Define the menu
menu = (
    pystray.MenuItem('Say Hello', show_message),
    pystray.MenuItem('Quit', quit_action)
)

# Create and run the icon
icon = pystray.Icon('my_app_name', icon_image, 'My Awesome App', menu)

# For simple, blocking execution, use icon.run()
# For integration with other GUI frameworks, consider icon.run_detached() in a separate thread.
# In this example, we'll use run() for simplicity.
print("pystray icon is running. Right-click for menu, or click 'Quit' to exit.")
icon.run()

view raw JSON →