ConsoleMenu — Standard menu class

class consolemenu.ConsoleMenu(title=None, subtitle=None, screen=None, formatter=None, prologue_text=None, epilogue_text=None, clear_screen=True, show_exit_option=True, exit_option_text='Exit', exit_menu_char=None)[source]

A class that displays a menu and allows the user to select an option.

Parameters:
  • title (str) – The title of the menu, or a method reference that returns a string.
  • subtitle (str) – The subtitle of the menu, or a method reference that returns a string.
  • screen (consolemenu.screen.Screen) – The screen object associated with this menu.
  • formatter (MenuFormatBuilder) – The MenuFormatBuilder instance used to format this menu.
  • prologue_text (str) – Text or method reference to include in the “prologue” section of the menu.
  • epilogue_text (str) – Text or method reference to include in the “epilogue” section of the menu.
  • show_exit_option (bool) – Specifies whether this menu should show an exit item by default. Defaults to True. Can be overridden when the menu is started.
  • exit_option_text (str) – Text for the Exit menu item. Defaults to ‘Exit’.
  • exit_menu_char (str) – Character to use for exiting the menu. Defaults to None.
  • clear_screen (bool) – Set to False to disable clearing of screen between menus
cls.currently_active_menu

Class variable that holds the currently active menu or None if no menu is currently active (e.g. when switching between menus)

Type:ConsoleMenu
items

The list of MenuItems that the menu will display

Type:list of MenuItem
parent

The parent of this menu

Type:ConsoleMenu
previous_active_menu

the previously active menu to be restored into the class’s currently active menu

Type:ConsoleMenu
current_option

The currently highlighted menu option

Type:int
selected_option

The option that the user has most recently selected

Type:int
current_item

The item corresponding to the menu option that is currently highlighted, or None.

Type:consolemenu.items.MenuItem
selected_item

The item in items that the user most recently selected, or None.

Type:consolemenu.items.MenuItem
start(show_exit_option=None)[source]

Start the menu in a new thread and allow the user to interact with it. The thread is a daemon, so join() should be called if there’s a possibility that the main thread will exit before the menu is done

Parameters:show_exit_option (bool) – Specify whether the exit item should be shown, defaults to the value set in the constructor
join(timeout=None)[source]

Should be called at some point after start() to block until the menu exits.

Parameters:timeout (Number) – How long to wait before timing out.
show(show_exit_option=None)[source]

Calls start and then immediately joins.

Parameters:show_exit_option (bool) – Specify whether the exit item should be shown, defaults to the value set in the constructor

Item Management

append_item(item)[source]

Add an item to the end of the menu before the exit item.

Parameters:item (MenuItem) – The item to be added.
add_exit()[source]

Add the exit item if necessary. Used to make sure there aren’t multiple exit items.

Returns:True if item needed to be added, False otherwise.
Return type:bool
remove_exit()[source]

Remove the exit item if necessary. Used to make sure we only remove the exit item, not something else.

Returns:True if item needed to be removed, False otherwise.
Return type:bool

User interaction

get_input()[source]

Can be overridden to change the input method. Called in process_user_input()

Returns:the ordinal value of a single character
Return type:int
process_user_input()[source]

Gets the next single character and decides what to do with it

draw()[source]

Refresh the screen and redraw the menu. Should be called whenever something changes that needs to be redrawn.

go_to(option)[source]

Go to the option entered by the user as a number

Parameters:option (int) – the option to go to
go_up()[source]

Go up one, wrap to end if necessary

go_down()[source]

Go down one, wrap to beginning if necessary

select()[source]

Select the current item and run it

exit()[source]

Signal the menu to exit, then block until it’s done cleaning up

State management

is_alive()[source]

Check whether the thread is stil alive.

Returns:True if the thread is still alive; False otherwise.
Return type:bool
wait_for_start(timeout=None)[source]

Block until the menu is started.

Parameters:timeout – How long to wait before timing out.
Returns:False if timeout is given and operation times out, True otherwise. None before Python 2.7.
Return type:bool
pause()[source]

Temporarily pause the menu until resume is called.

resume()[source]

Sets the currently active menu to this one and resumes it.

is_running()[source]

Check if the menu has been started and is not paused.

Returns:True if the menu is started and hasn’t been paused; False otherwise.
Return type:bool