I thought someone might find this useful. Also, feedback on code style ('the python way of doing things') would be appreciated, I've been coding in Python for less than 12 hours.
It is a way to present a simple menu with submenus, and returns the selections for further processing. The example provided is from a food diary script I'm converting from Workflow that I used to use with Day One.
I know there is no kind of error checking at all, for my purposes I don't really think that's necessary.
Should work in Pythonista too.
```
import dialogs
def menuWithSubmenus(menuTitle, menuList):
#Present main menu and get user choice
mainMenu = ([d['mainMenuOption'] for d in menuList])
choice = dialogs.list_dialog(title=menuTitle, items=mainMenu, multiple=False)
#Present sub-menu and get user choice
subMenu = (next((item for item in menuList if item['mainMenuOption'] == choice))['subMenuOptions'])
subChoice = dialogs.list_dialog(title=menuTitle, items=subMenu, multiple=False)
return { 'mainMenuOption':choice, 'subMenuOption': subChoice }
Example Usage
menuItems = (
{ 'mainMenuOption':'Cereal', 'subMenuOptions': ('Saltana Bran', 'Wheat Biscuits') },
{ 'mainMenuOption':'Fruit', 'subMenuOptions': ('Apple', 'Banana', 'Mandarine') }
)
print (menuWithSubmenus('Meal/Snack Type',menuItems))