Go to the first, previous, next, last section, table of contents.


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.

Screen Mixin: screenMenu

Provides the menu window for each screen. The look of the window is controlled by the following class variables:

  • Variable Default Description
  • menu_fontname 9x15bold Font for menu options.
  • menu_foreground black Foreground color for the menu window.
  • menu_background white Background color for the menu window.
  • menu_borderwidth 3 Border for the men window.
  • menu_handler MenuKeyHandler Keyboard handler for menus.
    Method: screenMenu menu_make ( labels, align = 'center' )
    Creates a menu window from labels, which must be a sequence of strings. The strings will be aligned in the window according to the value of align, which may be 'left', 'right' or 'center'. The width and height of the resulting window are returned as a tuple for use in calculating the menu placement.
    Method: screenMenu menu_run ( x, y, action )
    x and y are the coordinates the menu should be placed at. action is a callable argument that will be invoked with the string used for the label the user selected. If the user aborts the menu, action will not be invoked.
    A simple example of a menu with dictionary of functions might be:
    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:
    Class: MenuKeyHandler
    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
    
    Class: MenuCharHandler
    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.
    To have menus on your screen use your menu keys, you would add the 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.