pmbarrett314 / curses-menu

A simple console menu system in python using the curses library
MIT License
471 stars 55 forks source link

get command items into submenu #33

Closed Yolotroll101 closed 3 years ago

Yolotroll101 commented 6 years ago

I am trying to get multiple command items into a submenu. As far as I can tell, this is impossible. Please either tell me if it is possible, and if so, how, or if you are going to add it if it's not currently possible.

Yolotroll101 commented 6 years ago

I say that I think it's impossible because this happens in console:

AttributeError: 'CommandItem' object has no attribute 'start'
nickcounts commented 5 years ago

Not sure if you sorted this out, but it is possible. You need to add a submenu to its parent as a SubmenuItem, which takes 3 arguments:

Here's a sample:

from cursesmenu import CursesMenu
from cursesmenu.items import *

menu = CursesMenu("TOP-LEVEL", "It has a subtitle too!")

command_item  = CommandItem("Run a console command", "touch hello.txt")
function_item = FunctionItem("Call a function", input, ["Enter some input"])

submenu       = CursesMenu("SUBMENU", "Submenu Subtitle String")
submenu.append_item(CommandItem("Make hello.txt", "touch hello.txt"))
submenu.append_item(CommandItem("Make world.txt", "touch world.txt"))
submenu.add_exit()

# Create the SubmenuItem object that will become selectable in the top-level menu
subMenuItemInTopMenu = SubmenuItem("Go to submenu", submenu, menu)

# Add 2 normal items and a submenu to the top-level menu
menu.append_item(command_item)
menu.append_item(function_item)
menu.append_item(subMenuItemInTopMenu)
menu.add_exit()

menu.show()