Home Blog CV Projects Patterns Notes Book Colophon Search

Python macOS Menu App

1 Oct, 2023

hello.py:

import PIL.Image
import PIL.ImageDraw
import pystray


def create_image(width, height, color1, color2):
    image = PIL.Image.new("RGB", (width, height), color1)
    dc = PIL.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


state = False


def on_clicked(icon, item):
    global state
    state = not item.checked


def quit_window(icon):
    icon.stop()


icon = pystray.Icon(
    "test",
    create_image(256, 256, "black", "white"),
    menu=pystray.Menu(
        pystray.MenuItem("Checkable", on_clicked, checked=lambda item: state),
        pystray.MenuItem("Quit", quit_window),
    ),
).run()

pyproject.toml:

[project]
name = "hello"
version = "0.1.0"
dependencies = [
  'pyinstaller',
  'pystray',
  'pillow',
]

[project.optional-dependencies]
dev = [
  'black==23.9.1',
  'uvicorn==0.23.2',
  'isort==5.12.0',
  'autoflake==2.2.1',
]

README.md

pip install .
rm -r dist && .venv/bin/pyinstaller --windowed hello.py
open dist/hello.app

Comments

Be the first to comment.

Add Comment





Copyright James Gardner 1996-2020 All Rights Reserved. Admin.