Skip to main content

Windows system tray icon

Project description

lynxtray

This module implements a Windows system tray icon with a right-click context menu.

Installation

To install lynxtray, run:

pip install lynxtray

Alternatively, you can use easy_install.

Usage

Creating an icon with one option in the context menu:

from lynxtray import SysTrayIcon
def say_hello(systray: SysTrayIcon) -> None:
	print("Hello, World!")
menu_options = (("Say Hello", None, say_hello),)
systray = SysTrayIcon("icon.ico", "Example tray icon", menu_options)
systray.start()

The first parameter to SysTrayIcon is a path to the icon to show in the systray. If the icon is not found, or if None is specified, a default system icon will be displayed. The second parameter is the hover text to show when the mouse is hovered over the systray icon. The traybar will run in its own thread, so the using script can continue to run.

The icon and/or hover text can be updated using the update() method with the appropriate hover_text or icon keyword argument:

for item in ['item1', 'item2', 'item3']:
	systray.update(hover_text=item)
	do_something(item)

To destroy the icon when the program ends, call

systray.shutdown()

SysTrayIcon can be used as a context manager to start and shutdown the tray, which also prevents hung tray threads should the parent thread fail or otherwise not close the tray process:

with SysTrayIcon(icon, hover_text) as systray:
	for item in ['item1', 'item2', 'item3']:
		systray.update(hover_text=item)
		do_something(item)

A "Quit" command is always appended to the end of the icon context menu, after the menu options specified by the user. To perform operations when Quit is selected, pass "on_quit=callback" as a parameter, e.g.:

def on_quit_callback(systray: SysTrayIcon) -> None:
	program.shutdown()
systray = SysTrayIcon("icon.ico", "Example tray icon", menu_options, on_quit=on_quit_callback)

When the user double-clicks the systray icon, the first option specified in menu_options will be executed. The default command may be changed to a different option by setting the parameter "default_menu_index", e.g.:

systray = SysTrayIcon("icon.ico", "Example tray icon", menu_options, default_menu_index=2)

menu_options must be a list of 3-tuples. Each 3-tuple specifies a context menu options. The first value in each tuple is the context menu string. Some versions of Windows can show icons next to each option in the context menu. This icon can be specified in the second value of the tuples. If None is passed, no icon is displayed for the option. The third value is the command to execute when the context menu is selected by the user.

It is possible to create sub-menus in the context menu by recursively passing a list of 3-tuple options as the third value of an option, instead of passing a callback function. e.g.

from lynxtray import SysTrayIcon
hover_text = "SysTrayIcon Demo"
def hello(sysTrayIcon: SysTrayIcon) -> None:
	print("Hello World.")
def simon(sysTrayIcon: SysTrayIcon) -> None:
	print("Hello Simon.")
def bye(sysTrayIcon: SysTrayIcon) -> None:
	print("Bye, then.")
def do_nothing(sysTrayIcon: SysTrayIcon) -> None:
	pass
menu_options = (
	('Say Hello', "hello.ico", hello),
	('Do nothing', None, do_nothing),
	('A sub-menu', "submenu.ico", (('Say Hello to Simon', "simon.ico", simon), ('Do nothing', None, do_nothing)))
)
sysTrayIcon = SysTrayIcon("main.ico", hover_text, menu_options, on_quit=bye, default_menu_index=1)
sysTrayIcon.start()

Note that in the previous examples, if no code is executed after calling systray.start(), the main thread will exit and the icon thread will continue to exist until the Quit option is selected. In order to catch keyboard interrupts, some code must be written that will call systray.shutdown when the program should quit. Using SysTrayIcon as a context manager automates the start and shutdown of the tray.

This module can only be used in Windows systems, otherwise the import statement will fail.

Credit

This module is adapted from an implementation by Simon Brunning, which in turn was adapted from Mark Hammond's win32gui_taskbar.py and win32gui_menu.py demos from PyWin32.

Installation

Development docs

https://packaging.python.org/en/latest/tutorials/packaging-projects/

Build

python -m build

Install dev version

python -m pip install --user --break-system-packages .

Upload to PyPI

First build the package as per Build.

Then create the config file ~/.pypirc:

[pypi]
	username = __token__
	password = pypi-ABCDEFGHTOKEN

Then upload the package:

# Testing PyPI
python -m twine upload --repository testpypi dist/*
# Prod PyPI
python -m twine upload dist/*

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lynxtray-0.2.2.tar.gz (21.3 kB view details)

Uploaded Source

Built Distribution

lynxtray-0.2.2-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file lynxtray-0.2.2.tar.gz.

File metadata

  • Download URL: lynxtray-0.2.2.tar.gz
  • Upload date:
  • Size: 21.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.7

File hashes

Hashes for lynxtray-0.2.2.tar.gz
Algorithm Hash digest
SHA256 f6490c89ed3b1c54e75980421d58eb5ff5b728aaee48a14c6537fa673a8b7786
MD5 a7b0befad9628effd0a37df4718d1545
BLAKE2b-256 d8e90c3e2a85ce90610e30c05ea981b36531193dc831bd0c2f6919a46e04a124

See more details on using hashes here.

File details

Details for the file lynxtray-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: lynxtray-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 9.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.7

File hashes

Hashes for lynxtray-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 5e6e6c909cdc0b849e2165e42d3975d2a429f368eeaeeb58f86bd02f8412bc08
MD5 f49e456913541498b4ebafb64c77e4b5
BLAKE2b-256 b062357c7e358d70d64c9b74c37e4686a9f3ffbae7194a57634f3e8ce98d49bd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page