Menu UI element¶
from ui import Menu ... menu_contents = [ ["Do this", do_this], ["Do this with 20", lambda: do_this(x=20)], ["Do nothing"], ["My submenu", submenu.activate] ] Menu(menu_contents, i, o, "My menu").activate()
Menu(contents, i, o, name='Menu', entry_height=1, append_exit=True, catch_exit=True, exitable=True, contents_hook=None, scrolling=True)¶
Implements a menu which can be used to navigate through your application, output a list of values or select actions to perform. Is one of the most used elements, used both in system core and in most of the applications.
contents: list of menu elements which was passed either to
Menuconstructor or to
- Menu element structure is a list, where:
element(element’s representation) is either a string, which simply has the element’s value as it’ll be displayed, such as “Menu element 1”, or, in case of entry_height > 1, can be a list of strings, each of which represents a corresponding display row occupied by the element.
element(element’s callback) is a function which is called when menu’s element is activated (such as pressing ENTER button when menu’s element is selected). * Can be omitted if you don’t need to have any actions taken upon activation of the element. * Can be specified as ‘exit’ if you want a menu element that exits the menu upon activation.
If you want to set contents after the initalisation, please, use set_contents() method.
_contents: “Working copy” of menu contents, basically, a
contentsattribute which has been processed by
pointer: currently selected menu element’s number in
in_background: a flag which indicates if menu is currently active, either if being displayed or being in background (for example, if a sub-menu of this menu is currently active)
in_foreground: a flag which indicates if menu is currently displayed. If it’s not active, inhibits any of menu’s actions which can interfere with other menu or UI element being displayed.
first_displayed_entry: Internal pointer which points to the number of
self._contentselement which is at the topmost position of the menu as it’s currently displayed on the screen
last_displayed_entry: Internal pointer which points to the number of
self._contentselement which is at the lowest position of the menu as it’s currently displayed on the screen
no_entry_message: The entry displayed in case menu has no elements
__init__(contents, i, o, name='Menu', entry_height=1, append_exit=True, catch_exit=True, exitable=True, contents_hook=None, scrolling=True)¶
Initialises the Menu object.
contents: a list of values, which can be constructed as described in the Menu object’s docstring.
o: input&output device objects
name: Menu name which can be used internally and for debugging.
entry_height: number of display rows one menu element occupies.
append_exit: Appends an “Exit” alement to menu elements. Doesn’t do it if any of elements has callback set as ‘exit’.
MenuExitExceptionis received and catch_exit is False, it passes
MenuExitExceptionto the parent menu so that it exits, too. If catch_exit is True, MenuExitException is not passed along.
exitable: Decides if menu can exit at all by pressing
KEY_LEFT. Set by default and disables
KEY_LEFTcallback if unset. Is used for pyLCI main menu, not advised to be used in other settings.
A method which is called when menu needs to start operating. Is blocking, sets up input&output devices, renders the menu and waits until self.in_background is False, while menu callbacks are executed from the input device thread. This method also raises MenuExitException if menu exited due to it and
catch_exitis set to False.
Deactivates the menu completely, exiting it. As for now, pointer state is preserved through menu activations/deactivations
Sets the keymap. In future, will allow per-system keycode-to-callback tweaking using a config file.
A debug method. Useful for hooking up to an input event so that you can see the representation of menu’s contents.
A debug method. Useful for hooking up to an input event so that you can see which menu is currently processing input events.
Sets the menu contents, as well as additionally re-sets
first_displayed_entrypointers and calculates the value for
An exception that you can throw from a menu callback to exit the menu that callback was called from