aegirhall / console-menu

A simple Python menu system for building terminal user interfaces.
MIT License
365 stars 58 forks source link

How to get the index from the selection menu/item? #58

Open funcode2 opened 3 years ago

funcode2 commented 3 years ago

Hi there! I like this console-menu project.

Can anyone give an example how I can get the index I selected from the submenu. It can be print out after it is back to the top menu.

Here is the code for the menu. Many thanks!

from consolemenu import from consolemenu.items import

def action(name): pass

def main():

# Create the root menu
menu = ConsoleMenu("Root Menu", "This is the Root Menu Subtitle")

submenu = SelectionMenu(["item1", "item2", "item3"], title="Selection Menu",
                        subtitle="These menu items return to the previous menu")  # Customize the exit text

submenu_item = SubmenuItem("Submenu item", submenu=submenu)

#selection_item = SelectionItem("Selection Item", submenu)

#submenu_item.set_menu(submenu)
# Add all the items to the root menu
menu.append_item(submenu_item)

# Show the menu
menu.start()
menu.join()

if name == "main": main()

MoralCode commented 2 years ago

the get_selection function of SelectionMenu seems to use menu.selected_option. source: https://console-menu.readthedocs.io/en/latest/_modules/consolemenu/selection_menu.html#SelectionMenu