usage.rst 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. Creating a *system tray icon*
  2. -----------------------------
  3. In order to create a *system tray icon*, the class ``pystray.Icon`` is used::
  4. import pystray
  5. icon = pystray.Icon('test name')
  6. In order for the icon to be displayed, you must provide an icon. This icon must
  7. be specified as a ``PIL.Image.Image``::
  8. from PIL import Image, ImageDraw
  9. def create_image():
  10. # Generate an image and draw a pattern
  11. image = Image.new('RGB', (width, height), color1)
  12. dc = ImageDraw.Draw(image)
  13. dc.rectangle(
  14. (width // 2, 0, width, height // 2),
  15. fill=color2)
  16. dc.rectangle(
  17. (0, height // 2, width // 2, height),
  18. fill=color2)
  19. return image
  20. icon.icon = create_image()
  21. To finally show you icon, run the following code::
  22. icon.run()
  23. The call to ``pystray.Icon.run()`` is blocking, and it must be performed from
  24. the main thread of the application. The reason for this is that the *system tray
  25. icon* implementation for *OSX* will fail unless called from the main thread, and
  26. it also requires the application runloop to be running. ``pystray.Icon.run()``
  27. will start the runloop.
  28. The ``run()`` method accepts an optional argument: ``setup``, a callable.
  29. The ``setup`` funciton will be run in a separate thread once the *system tray
  30. icon* is ready. The icon does not wait for it to complete, so you may put any
  31. code that would follow the call to ``pystray.Icon.run()`` in it.
  32. The call to ``pystray.Icon.run()`` will not complete until ``stop()`` is called.
  33. Getting input from the *system tray icon*
  34. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  35. In order to receive notifications about user interaction with the icon, a
  36. popup menu can be added with the ``menu`` constructor argument.
  37. This must be an instance of ``pystray.Menu``. Please see the reference for more
  38. information about the format.
  39. It will be displayed when the right-hand button has been pressed on the icon on
  40. *Windows*, and when the icon has been clicked on other platforms. Menus are not
  41. supported on *X*.
  42. Menus also support a default item. On *Windows*, and *X*, this item will be
  43. activated when the user clicks on the icon using the primary button. On other
  44. platforms it will be activated if the menu contains no visible entries; it does
  45. not have to be visible.
  46. All properties of menu items, except for the callback, can be dynamically
  47. calculated by supplying callables instead of values to the menu item
  48. constructor. The properties are recalculated every time the icon is clicked or
  49. any menu item is activated.
  50. If the dynamic properties change because of an external event, you must ensure
  51. that ``Icon.update_menu`` is called. This is required since not all supported
  52. platforms allow for the menu to be generated when displayed.
  53. Creating the menu
  54. ~~~~~~~~~~~~~~~~~
  55. *This is not supported on Xorg; please check Icon.HAS_MENU at runtime for
  56. support on the current platform.*
  57. A menu can be attached to a system tray icon by passing an instance of
  58. :class:`pystray.Menu` as the ``menu`` keyword argument.
  59. A menu consists of a list of menu items, optionally separated by menu
  60. separators.
  61. Separators are intended to group menu items into logical groups. They will not
  62. be displayed as the first and last visible item, and adjacent separators will be
  63. hidden.
  64. A menu item has several attributes:
  65. *text* and *action*
  66. The menu item text and its associated action.
  67. These are the only required attributes. Please see *submenu* below for
  68. alternate interpretations of *action*.
  69. *checked*
  70. Whether the menu item is checked.
  71. This can be one of three values:
  72. ``False``
  73. The item is decorated with an unchecked check box.
  74. ``True``
  75. The item is decorated with a checked check box.
  76. ``None``
  77. There is no hint that the item is checkable.
  78. If you want this to actually be togglable, you must pass a callable that
  79. returns the current state::
  80. from pystray import Icon as icon, Menu as menu, MenuItem as item
  81. state = False
  82. def on_clicked(icon, item):
  83. global state
  84. state = not item.checked
  85. # Update the state in `on_clicked` and return the new state in
  86. # a `checked` callable
  87. icon('test', create_image(), menu=menu(
  88. item(
  89. 'Checkable',
  90. on_clicked,
  91. checked=lambda item: state))).run()
  92. *radio*
  93. *This is not supported on macOS; please check Icon.HAS_MENU_RADIO at
  94. runtime for support on the current platform.*
  95. Whether this is a radio button.
  96. This is used only if ``checked`` is ``True`` or ``False``, and only has a
  97. visual meaning. The menu has no concept of radio button groups::
  98. from pystray import Icon as icon, Menu as menu, MenuItem as item
  99. state = 0
  100. def set_state(v):
  101. def inner(icon, item):
  102. global state
  103. state = v
  104. return inner
  105. def get_state(v):
  106. def inner(item):
  107. return state == v
  108. return inner
  109. # Let the menu items be a callable returning a sequence of menu
  110. # items to allow the menu to grow
  111. icon('test', create_image(), menu=menu(lambda: (
  112. item(
  113. 'State %d' % i,
  114. set_state(i),
  115. checked=get_state(i),
  116. radio=True)
  117. for i in range(max(5, state + 2))))).run()
  118. *default*
  119. *This is not supported on Darwin and using AppIndicator; please check
  120. Icon.HAS_DEFAULT at runtime for support on the current platform.*
  121. Whether this is the default item.
  122. It is drawn in a distinguished style and will be activated as the default
  123. item on platforms that support default actions. On *X*, this is the only
  124. action available.
  125. *visible*
  126. Whether the menu item is visible.
  127. *enabled*
  128. Whether the menu item is enabled. Disabled menu items are displayed, but are
  129. greyed out and cannot be activated.
  130. *submenu*
  131. The submenu, if any, that is attached to this menu item. Either a submenu
  132. or an action can be passed as the second argument to the constructor.
  133. The submenu must be an instance of :class:`Menu`::
  134. from pystray import Icon as icon, Menu as menu, MenuItem as item
  135. icon('test', create_image(), menu=menu(
  136. item(
  137. 'With submenu',
  138. menu(
  139. item(
  140. 'Submenu item 1',
  141. lambda icon, item: 1),
  142. item(
  143. 'Submenu item 2',
  144. lambda icon, item: 2))))).run()
  145. Once created, menus and menu items cannot be modified. All attributes except for
  146. the menu item callbacks can however be set to callables returning the current
  147. value. This also applies to the sequence of menu items belonging to a menu: this
  148. can be a callable returning the current sequence.
  149. Displaying notifications
  150. ~~~~~~~~~~~~~~~~~~~~~~~~
  151. *This is not supported on macOS and Xorg; please check Icon.HAS_NOTIFICATION
  152. at runtime for support on the current platform.*
  153. To display a system notification, use :meth:`pystray.Icon.notify`::
  154. from pystray import Icon as icon, Menu as menu, MenuItem as item
  155. icon('test', create_image(), menu=menu(
  156. item(
  157. 'With submenu',
  158. menu(
  159. item(
  160. 'Show message',
  161. lambda icon, item: icon.notify('Hello World!')),
  162. item(
  163. 'Submenu item 2',
  164. lambda icon, item: icon.remove_notification()))))).run()
  165. Selecting a backend
  166. -------------------
  167. *pystray* aims to provide a unified *API* for all supported platforms. In some
  168. cases, however, that is not entirely possible.
  169. This library supports a number of backends. On *macOS* and *Windows*, the
  170. operating system has system tray icons built-in, so the default backends should
  171. be used, but on *Linux* you may have to make a decision depending on your
  172. needs.
  173. By setting the environment variable ``PYSTRAY_BACKEND`` to one of the strings in
  174. the next section, the automatic selection is turned off.
  175. Supported backends
  176. ~~~~~~~~~~~~~~~~~~
  177. *appindicator*
  178. This is one of the backends available on *Linux*, and is the preferred
  179. choice. All *pystray* features except for a menu default action are
  180. supported, and if the *appindicator* library is installed on the system
  181. and the desktop environment supports it, the icon is guaranteed to be
  182. displayed.
  183. *darwin*
  184. This is the default backend when running on *macOS*. All *pystray* features
  185. are available.
  186. *gtk*
  187. This is one of the backends available on *Linux*, and is prioritised above
  188. the *XOrg* backend. It uses *GTK* as underlying library. All *pystray*
  189. features are available, but it may not actually result in a visible icon:
  190. when running a *gnome-shell* session, an third party plugin is required to
  191. display legacy tray icons.
  192. *win32*
  193. This is the default backend when running on *Windows*. All *pystray*
  194. features are available.
  195. *xorg*
  196. This is one of the backends available on *Linux*. It is used as a fallback
  197. when no other backend can be loaded. It does not support any menu
  198. functionality except for a default action.