### pystray.Icon.run() Method Reference Source: https://github.com/moses-palmer/pystray/blob/master/docs/usage.rst This section details the behavior and parameters of the pystray.Icon.run() method. It explains its blocking nature, the requirement to be called from the main thread on OSX, and the optional 'setup' callable argument for executing code once the icon is ready. ```APIDOC pystray.Icon.run(setup: callable = None) Description: Starts the system tray icon and its runloop. This call is blocking. Parameters: setup: A callable function that will be executed in a separate thread once the icon is ready. Behavior: - Blocking call; must be performed from the main thread on OSX. - Starts the application runloop. - Will not complete until pystray.Icon.stop() is called. - On Windows, calling from a non-main thread is safe. ``` -------------------------------- ### Partial Example: Radio Button Menu Item Source: https://github.com/moses-palmer/pystray/blob/master/docs/usage.rst This is a partial Python code snippet showing the beginning of an example for creating a radio button menu item. It illustrates the initial setup for managing state for a radio button group, though the full implementation is not provided. ```Python from pystray import Icon as icon, Menu as menu, MenuItem as item state = 0 def set_state(v): ``` -------------------------------- ### Python pystray Menu Item with Nested Submenu Source: https://github.com/moses-palmer/pystray/blob/master/docs/usage.rst This example demonstrates how to create a system tray icon with a menu item that contains a nested submenu. It shows the structure for defining multiple levels of menu items using `pystray.Menu` and `pystray.MenuItem`. ```Python from pystray import Icon as icon, Menu as menu, MenuItem as item icon('test', create_image(), menu=menu( item( 'With submenu', menu( item( 'Submenu item 1', lambda icon, item: 1), item( 'Submenu item 2', lambda icon, item: 2))))).run() ``` -------------------------------- ### Create a Checkable pystray Menu Item Source: https://github.com/moses-palmer/pystray/blob/master/docs/usage.rst This Python example demonstrates how to create a menu item with a dynamically togglable 'checked' state. It uses a global variable to manage the state and a lambda function for the 'checked' attribute, which returns the current state when the menu is displayed. ```Python from pystray import Icon as icon, Menu as menu, MenuItem as item state = False def on_clicked(icon, item): global state state = not item.checked # Update the state in `on_clicked` and return the new state in # a `checked` callable icon('test', create_image(), menu=menu( item( 'Checkable', on_clicked, checked=lambda item: state))).run() ``` -------------------------------- ### Run pystray icon mainloop in a separate thread for framework integration Source: https://github.com/moses-palmer/pystray/blob/master/docs/faq.rst When integrating pystray with other GUI frameworks, the `run_detached` method may not always be suitable, especially outside of macOS. This example demonstrates how to run the pystray icon's mainloop in a separate thread, allowing the main thread to manage the toolkit's mainloop. This approach ensures compatibility across various platforms and frameworks. ```Python import pystray import threading import some_toolkit # Create the icon icon = pystray.Icon( 'test name', icon=create_icon()) # Run the icon mainloop in a separate thread threading.Thread(target=icon.run).start() # Run the toolkit mainlon in the main thread some_toolkit.mainloop() ``` -------------------------------- ### APIDOC pystray Supported Backends Source: https://github.com/moses-palmer/pystray/blob/master/docs/usage.rst Documentation for the various backends supported by pystray, detailing their platform applicability, feature support, and any specific requirements or limitations. It covers `appindicator`, `darwin`, `gtk`, `win32`, and `xorg`. ```APIDOC *appindicator* This is one of the backends available on *Linux*, and is the preferred choice. All *pystray* features except for a menu default action are supported, and if the *appindicator* library is installed on the system and the desktop environment supports it, the icon is guaranteed to be displayed. If the *appindicator* library is not available on the system, a fallback on *ayatana-appindicator* is attempted. *darwin* This is the default backend when running on *macOS*. All *pystray* features are available. *gtk* This is one of the backends available on *Linux*, and is prioritised above the *XOrg* backend. It uses *GTK* as underlying library. All *pystray* features are available, but it may not actually result in a visible icon: when running a *gnome-shell* session, an third party plugin is required to display legacy tray icons. *win32* This is the default backend when running on *Windows*. All *pystray* features are available. *xorg* This is one of the backends available on *Linux*. It is used as a fallback ``` -------------------------------- ### pystray.Menu Class API Documentation Source: https://github.com/moses-palmer/pystray/blob/master/docs/reference.rst API documentation for the `pystray.Menu` class, used to define context menus for system tray icons. This entry details all public methods and properties for menu creation, organization, and management of menu items. ```APIDOC Class: pystray.Menu Description: Represents a context menu associated with a system tray icon. Members: - All public methods and properties are documented. ``` -------------------------------- ### Create and Run a Basic pystray System Tray Icon Source: https://github.com/moses-palmer/pystray/blob/master/docs/usage.rst This snippet demonstrates how to initialize a pystray.Icon with a dynamically generated image and display it in the system tray. It includes a helper function to create a simple patterned image for the icon. The icon.run() call is blocking and must be on the main thread for macOS compatibility. ```Python import pystray from PIL import Image, ImageDraw def create_image(width, height, color1, color2): # Generate an image and draw a pattern 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 # In order for the icon to be displayed, you must provide an icon icon = pystray.Icon( 'test name', icon=create_image(64, 64, 'black', 'white')) # To finally show you icon, call run icon.run() ``` -------------------------------- ### APIDOC pystray.MenuItem Attributes Source: https://github.com/moses-palmer/pystray/blob/master/docs/usage.rst Documentation for key attributes of a pystray MenuItem, including `default` for distinguishing items, `visible` for visibility control, `enabled` for interactivity, and `submenu` for attaching nested menus. It highlights platform-specific support and usage constraints for each attribute. ```APIDOC *default* Whether this is the default item. This is not supported on Darwin and using AppIndicator; please check Icon.HAS_DEFAULT at runtime for support on the current platform. It is drawn in a distinguished style and will be activated as the default item on platforms that support default actions. On *X*, this is the only action available. *visible* Whether the menu item is visible. *enabled* Whether the menu item is enabled. Disabled menu items are displayed, but are greyed out and cannot be activated. *submenu* The submenu, if any, that is attached to this menu item. Either a submenu or an action can be passed as the second argument to the constructor. The submenu must be an instance of :class:`Menu`:: or a tuple of menu entries. ``` -------------------------------- ### pystray.Menu and MenuItem API Overview Source: https://github.com/moses-palmer/pystray/blob/master/docs/usage.rst This documentation describes how to add interactive menus to a pystray icon using pystray.Menu and its associated MenuItem attributes. It covers menu display behavior across platforms, the concept of a default menu item, and how to implement dynamic properties for menu items using callables. ```APIDOC pystray.Menu: Description: Used to define a popup menu for a system tray icon. Usage: Passed as the 'menu' keyword argument to pystray.Icon. Behavior: - Displayed on right-click (Windows) or click (other platforms). - Not supported on Xorg; check Icon.HAS_MENU at runtime. - Supports a default item activated by primary click (Windows, X) or if no visible entries (other platforms). Dynamic Properties: - Menu item properties (except callback) can be callables for dynamic calculation. - Icon.update_menu must be called if dynamic properties change due to external events. pystray.MenuItem Attributes: text: The display text of the menu item. Required. action: The callable action associated with the menu item. Required. (Can be alternate interpretation for submenus). checked: Controls the check box decoration of the item. - False: Unchecked check box. - True: Checked check box. - None: No hint that the item is checkable. - Can be a callable that returns the current state for toggling. radio: (Not supported on macOS; check Icon.HAS_MENU_RADIO at runtime) - Whether this is a radio button. - Used only if 'checked' is True or False. - Has visual meaning only; menu has no concept of radio button groups. ``` -------------------------------- ### pystray.Icon Class API Documentation Source: https://github.com/moses-palmer/pystray/blob/master/docs/reference.rst API documentation for the `pystray.Icon` class, which manages system tray icons. This entry details all public methods and properties available for interacting with the icon, such as displaying it, updating its image, or handling events. ```APIDOC Class: pystray.Icon Description: Represents a system tray icon in the system notification area. Members: - All public methods and properties are documented. ``` -------------------------------- ### Python pystray Dynamic Menu with State Management Source: https://github.com/moses-palmer/pystray/blob/master/docs/usage.rst This snippet demonstrates how to create a dynamic system tray menu where menu items can reflect and change a global state. It uses `set_state` and `get_state` functions to manage the checked status of radio buttons, allowing the menu to grow based on the current state. ```Python def inner(icon, item): global state state = v return inner def get_state(v): def inner(item): return state == v return inner icon('test', create_image(), menu=menu(lambda: ( item( 'State %d' % i, set_state(i), checked=get_state(i), radio=True) for i in range(max(5, state + 2))))).run() ``` -------------------------------- ### pystray.MenuItem Class API Documentation Source: https://github.com/moses-palmer/pystray/blob/master/docs/reference.rst API documentation for the `pystray.MenuItem` class, representing individual items within a `pystray.Menu`. This entry details all public methods and properties for defining menu item behavior, including text, actions, and state (e.g., checked, disabled). ```APIDOC Class: pystray.MenuItem Description: Represents an individual item within a pystray menu. Members: - All public methods and properties are documented. ``` -------------------------------- ### Python pystray Displaying System Notifications Source: https://github.com/moses-palmer/pystray/blob/master/docs/usage.rst This snippet illustrates how to display and remove system notifications using the `pystray.Icon.notify` and `pystray.Icon.remove_notification` methods. It integrates notification functionality into a system tray menu, allowing users to trigger messages or clear them. ```Python from pystray import Icon as icon, Menu as menu, MenuItem as item icon('test', create_image(), menu=menu( item( 'With submenu', menu( item( 'Show message', lambda icon, item: icon.notify('Hello World!')), item( 'Submenu item 2', lambda icon, item: icon.remove_notification()))))).run() ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.