I want to change the text for an item variably. to do this, i tried to update the menu using update_menu(). unfortunately, this didn't work and i couldn't find anything more detailed in the pystray documentation. I hope you can help me. thank you.
from pystray import Icon as icon, Menu as menu, MenuItem as item
import PIL.Image
image = PIL.Image.new('RGB', (100, 100), 255)
adapter = 'before'
def test():
global ps
global adapter
adapter = 'after'
ps.update_menu()
ps = icon(name='name', icon=image, menu=menu(
item(text='Adapter', action=menu(item(text='Test', action=test))),
item(text=adapter, action=lambda: test()),
)
)
ps.run()
I've got to a similar problem earlier, I wanted to update the submenu while running.
I made a version for what you wanted to adjust:
The MenuItem takes
(*items)
so giving it a list with items in a lambda function works to update it later.You have to alter the menu_items list to your needs and update.
Couldn't figure out tho how to just adjust the text, with a lambda e.g. either.
Found also this
example #17
, they changed the whole menu each time, but well...UPDATE: Here's what you wanted, cleaner than my previous answer:
found here