From 72c19e63dfb4501349f99112f0febb87fbd9ae55 Mon Sep 17 00:00:00 2001 From: MagTun <15843700+MagTun@users.noreply.github.com> Date: Fri, 5 Apr 2019 10:27:43 +0200 Subject: [PATCH 1/3] feature added: update of menu options --- src/infi/systray/traybar.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/infi/systray/traybar.py b/src/infi/systray/traybar.py index fe12497..ca93cf9 100755 --- a/src/infi/systray/traybar.py +++ b/src/infi/systray/traybar.py @@ -122,15 +122,25 @@ def shutdown(self): PostMessage(self._hwnd, WM_CLOSE, 0, 0) self._message_loop_thread.join() - def update(self, icon=None, hover_text=None): - """ update icon image and/or hover text """ + def update(self, icon=None, hover_text=None, menu_options=None): # "menu_options=None" added to be allow the update of the menu options + """ update icon image and/or hover text and/or menu options""" if icon: self._icon = icon self._load_icon() if hover_text: self._hover_text = hover_text + # "if menu_options" added to be allow the update of the menu options + if menu_options: + menu_options = menu_options or () + menu_options = menu_options + (('Quit', None, SysTrayIcon.QUIT),) + self._next_action_id = SysTrayIcon.FIRST_ID + self._menu_actions_by_id = set() + self._menu_options = self._add_ids_to_menu_options(list(menu_options)) + self._menu_actions_by_id = dict(self._menu_actions_by_id) + self._menu = None # detroy the old menu created by right clicking the icon self._refresh_icon() + def _add_ids_to_menu_options(self, menu_options): result = [] for menu_option in menu_options: From d51b13ecfb57d6238ffb9e1aa4ff87e4888af9ca Mon Sep 17 00:00:00 2001 From: MagTun <15843700+MagTun@users.noreply.github.com> Date: Sun, 28 Apr 2019 20:29:32 +0200 Subject: [PATCH 2/3] comment and unnecessary line about menu_options removed --- src/infi/systray/traybar.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/infi/systray/traybar.py b/src/infi/systray/traybar.py index ca93cf9..de05a9d 100755 --- a/src/infi/systray/traybar.py +++ b/src/infi/systray/traybar.py @@ -122,7 +122,7 @@ def shutdown(self): PostMessage(self._hwnd, WM_CLOSE, 0, 0) self._message_loop_thread.join() - def update(self, icon=None, hover_text=None, menu_options=None): # "menu_options=None" added to be allow the update of the menu options + def update(self, icon=None, hover_text=None, menu_options=None): """ update icon image and/or hover text and/or menu options""" if icon: self._icon = icon @@ -131,7 +131,6 @@ def update(self, icon=None, hover_text=None, menu_options=None): # "menu_optio self._hover_text = hover_text # "if menu_options" added to be allow the update of the menu options if menu_options: - menu_options = menu_options or () menu_options = menu_options + (('Quit', None, SysTrayIcon.QUIT),) self._next_action_id = SysTrayIcon.FIRST_ID self._menu_actions_by_id = set() From 51f4977ef7e2249e0aeb2151b6fe6e3d24411f1b Mon Sep 17 00:00:00 2001 From: MagTun <15843700+MagTun@users.noreply.github.com> Date: Sun, 28 Apr 2019 21:13:40 +0200 Subject: [PATCH 3/3] Update README.md --- README.md | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 0a241da..2080455 100644 --- a/README.md +++ b/README.md @@ -12,12 +12,12 @@ Alternatively, you can use easy_install. Usage ----- -Creating an icon with one option in the context menu: +### Creating an icon with one option in the context menu: from infi.systray import SysTrayIcon def say_hello(systray): print "Hello, World!" - menu_options = (("Say Hello", None, say_hello),) + menu_options = (("Say Hello", "hello.ico", say_hello),) systray = SysTrayIcon("icon.ico", "Example tray icon", menu_options) systray.start() @@ -26,12 +26,29 @@ 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 the parameters of `menu_options`: cf the section **Menu** below + +### Updating : icon, hover text and/or menu options +The icon and/or hover text and/or menu options can be updated using the update() method with the appropriate `hover_text` or `icon` or `menu_options` keyword argument: + +#### Updating the hover_text: for item in ['item1', 'item2', 'item3']: systray.update(hover_text=item) do_something(item) +#### Updating the menu options: + + def say_hello(systray): + systray.update(menu_options=menu_optionsbye) + def say_bye(systray): + systray.update(menu_options=menu_optionshello) + menu_optionshello = (("Say Hello","hello.ico", say_hello),) + menu_optionsbye = (("Say Bye", "bye.ico", say_bye),) + systray = SysTrayIcon("icon.ico", "Hello/Bye", menu_optionshello) + systray.start() + +### Shutting down To destroy the icon when the program ends, call systray.shutdown() @@ -50,16 +67,23 @@ To perform operations when Quit is selected, pass "on_quit=callback" as a parame program.shutdown() systray = SysTrayIcon("icon.ico", "Example tray icon", menu_options, on_quit=on_quit_callback) +### Menu : +#### Parameters + + menu_options = (("Say Hello", "hello.ico", say_hello),) + +`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. +The second value is the icon (some versions of Windows can show icons next to each option in the context menu). 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. + +#### Default Menu index and Double click 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. +#### Sub-menus 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.