plugowski / umenu

Menu generator for MicroPython
20 stars 6 forks source link

adding items to a menu after MenuScreen is defined #7

Closed paukstelis closed 10 months ago

paukstelis commented 10 months ago

I'm trying to use this with some settings in json format. I would like to be able to add settings just by adding an entry into the json file. To do this more easily, I would like to iterate over the imported json list to add menu items. Interestingly I've found that if I declare and set the MenuScreen first, then add items, all ValueItems through a display fault.

i.e. this works:

menu.set_screen(MenuScreen('Settings')
                .add(ValueItem('Speed', 10, 1, 100, 1, print))
                .add(EnumItem("Mode", ['Option 1', 'Option 2'], print, 0))
) 

this doesn't:

menu.set_screen(MenuScreen('Settings'))
menu.main_screen.add(ValueItem('Speed', 10, 1, 100, 1, print))
menu.main_screen.add(EnumItem("Mode", ['Option 1', 'Option 2'], print, 0))

and it fails with the self.display being a NoneType object. Any thoughts on a way to rectify this? It seems to only impact ValueItem, since it has its own defined draw(). It does display the item in the main menu, it just throws the exception when it is selected.

paukstelis commented 10 months ago

i figured it out. CustomItem needs to have _update_display called: menu._update_display(menu.main_screen.__dict__['_items'])