Skip to content

Using the Menu class

Vincent Fabre edited this page May 10, 2022 · 2 revisions

Start with menus

first, import the Menu class:

from flamewok import Menu

create a menu:

menu = Menu()

add dialog optionS (as much as you want):

menu.add_boxes([
    <tuple or string>,
])

the tuples must be like this: (str(the choice to enter), str(the label of the box), a function) note that the function must be wrote without (), and then no parameters, it is just expected to drive the user into the menu.

to make the menu appear:

menu.ask()

About parameters

When creating a menu (menu=Menu(parameters)), it is possible to pass parameters to the menu to customize it. Defaults parameters are:

Menu(
inside_sep=": ",
sep=" | ",
box_size=20,
line_length=80,
prompt="?> ",
error_message="\n!! Wrong entry, you must enter a valid choice !!",
)

Manipulating an existing menu

The Menu class comes with a few public methods and attributes that allows to manipulate a menu:

Menu.boxes: the list where the menu items (see ActionBox and TextBox) are stored, you can manipulate it as any python list. It is the more convenient way to manipulate the menu, with the boxes class (see BOXES).

Menu.ask() # shows the menu to the user, and a prompt to get the user's choice.

Menu.add_boxes(list of tuples and strings) # see START WITH MENUS. Basic way to create all the menu items in a row.

Menu.add_box(a tuple or a string) # same as Menu.add_boxes(), but for only one item.

Menu.set_boxes(list of tuples and strings): use it the same way as Menu.add_boxes(), but erase all the former items instead of adding the new ones.

Boxes

There are 2 classes of boxes: ActionBox (for the actionable items) and TextBox (for simply some text in your menu). you can create a box directly from their class, and then insert them into the Menu.boxes (wich is a list).

ActionBox(choice, label, function)

choice: an int or str (it will be converted into str anyway), the user will have to enter it in the prompt to activate the item.

label: the label of the item, you can give an empty string if you want.

function: a function or method name (not a string !), without () and no parameters.

TextBox(label)

label: some string to display.

Clone this wiki locally