menu
Extension Module
The menu
module provides a screen mixin to display a menu of
options for the user to select from, as well as a pair of keyboard
handler templates for the menu. There is only one menu for each
screen, but the available options may be changed each time it is
displayed. To the user, there appear to be many menus, but only one
may be displayed at a time.
Provides the menu window for each screen. The look of the window is controlled by the following class variables:
'left'
, 'right'
or
'center'
. The width and height of the resulting
window are returned as a tuple for use in calculating the menu
placement.
class MyFunctionMenu: def __init__(self, screen, dict): self.dict = dict labels = dict.keys() labels.sort() width, height = screen.menu_make(labels) # Center the menu screen.menu_run((screen.root_width - width) / 2, (screen.root_height - height) / 2, self) def __call__(self, choice): self.dict[choice]()Making selections and aborting the menu are done via key handlers See section
keys
Extension Module, and two template key handlers are provided for menu
selections:
MenuKeyHandler
provides the methods _up
, _down
,
_do
and _abort
. These move the current selection, pass
the current selection to the action object passed to
menu_run
, and abort the menu taking no action. A binding with
Emacs keys might look like:
class MyMenuKeys(MenuKeyHandler): C_p = MenuKeyHandler._up C_n = MenuKeyHandler._down Return = MenuKeyHandler._do C_g = MenuKeyHandler._abort
MenuCharHandler
adds the _goto
method, which moves the
current selection to the first label that starts with the a character
greater than or equal to the typed key. It then binds the keys
a
to z
and 0
to 9
to _goto. This lets the
user select labels by their first character if MenuCharHandler
is used instead of MenuKeyHandler
.
screenMenu
mixin and set the menu_handler
class
variable:
class MyScreen(Screen, screenMenu): menu_handler = MyMenuKeys
Go to the first, previous, next, last section, table of contents.