Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating a Menu from a list works for Icon (main menu) but not for Menu (submenu) #140

Open
AnAnalogGuy opened this issue Apr 15, 2023 · 1 comment

Comments

@AnAnalogGuy
Copy link

AnAnalogGuy commented Apr 15, 2023

I am dynamically creating a menu including one submenu, both with a couple of items.
The submenu as well as the menu are created the same way as a list object and then being instanced as a pystray.Menu object.

The submenu

menu_intervals = []
for intervals_data in intervals:
    menu_items_checked_state[intervals.get(intervals_data)] = not MenuItem.checked
    menu_intervals.append(MenuItem(intervals.get(intervals_data), on_click_interval(Icon, intervals.get(intervals_data),
                        int(intervals_data)), default=False, checked=lambda MenuItem: menu_items_checked_state[MenuItem.text]))
    interval_menu_counter += 1

The Menu

menuItems = []
....
menuItems.append(MenuItem("Toggle Audio Feedback", clicked_toggle_audio))
menuItems.append(MenuItem("Web Interface", clicked_home))
menuItems.append(Menu.SEPARATOR)
menuItems.append(MenuItem("Check for Update", clicked_update))
....

And then adding it all together:

menuItems.append(MenuItem("Auto Position Change", Menu(menu_intervals)))
systray_icon = Icon("Application", image_default, menu=menuItems)

From the documentation:

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 Menu.

While creating and adding a Menu to Icon is working this way, it's running into a runtime error doing the same for Menu used as a submenu.

@AnAnalogGuy
Copy link
Author

AnAnalogGuy commented Apr 15, 2023

To make it short:

Instead of
menuItems.append(MenuItem("Auto Position Change", Menu(menu_intervals)))
it needs to be
menuItems.append(MenuItem("Auto Position Change", Menu(lambda: menu_intervals)))

To highlight the difference: Menu(lambda: menu_intervals)

@moses-palmer This is quite hard to understand from the docs. I'd like to suggest to add a related example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant