simple_menu - Draw a Menu

New in version 1.4.

To allow quickly hacking some scripts, Pycardium has a small library for displaying menus. You can use it like this:

import color
import simple_menu


class MyMenu(simple_menu.Menu):
    color_1 = color.CAMPGREEN
    color_2 = color.CAMPGREEN_DARK

    def on_select(self, name, index):
        print("{!r} was selected!".format(name))


if __name__ == "__main__":
    MyMenu(["foo", "bar", "baz"]).run()
class simple_menu.Menu(entries)[source]

A simple menu for card10.

This menu class is supposed to be inherited from to create a menu as shown in the example above.

To instanciate the menu, pass a list of entries to the constructor:

m = Menu(os.listdir("."))
m.run()

Then, call run() to start the event loop.

New in version 1.4.

color_1

Background color A.

color_2

Background color B.

color_text

Text color.

color_sel

Color of the selector.

scroll_speed = 0.5

Time to wait before scrolling to the right.

New in version 1.9.

timeout = None

Optional timeout for inactivity. Once this timeout is reached, on_timeout() will be called.

New in version 1.9.

right_buttons_scroll = False

Use top right and bottom right buttons to move in the list instead of bottom left and bottom right buttons.

on_scroll(item, index)[source]

Hook when the selector scrolls to a new item.

This hook is run everytime a scroll-up or scroll-down is performed. Overwrite this function in your own menus if you want to do some action every time a new item is scrolled onto.

Parameters
  • item – The item which the selector now points to.

  • index (int) – Index into the entries list of the item.

on_select(item, index)[source]

Hook when an item as selected.

The given index was selected with a SELECT button press. Overwrite this function in your menu to perform an action on select.

Parameters
  • item – The item which was selected.

  • index (int) – Index into the entries list of the item.

on_timeout()[source]

The inactivity timeout has been triggered. See simple_menu.Menu.timeout.

New in version 1.9.

exit()[source]

Exit the event-loop. This should be called from inside an on_* hook.

New in version 1.9.

Changed in version 1.11: Fixed this function not working properly.

entry2name(value)[source]

Convert an entry object to a string representation.

Overwrite this functio if your menu items are not plain strings.

Example:

class MyMenu(simple_menu.Menu):
    def entry2name(self, value):
        return value[0]

MyMenu(
    [("a", 123), ("b", 321)]
).run()
draw_entry(value, index, offset)[source]

Draw a single entry.

This is an internal function; you can override it for customized behavior.

Parameters
  • value – The value for this entry. Use this to identify different entries.

  • index (int) – A unique index per entry. Stable for a certain entry, but not an index into entries.

  • offset (int) – Y-offset for this entry.

draw_menu(offset=0)[source]

Draw the menu.

You’ll probably never need to call this yourself; it is called automatially in the event loop (run()).

error(line1, line2='')[source]

Display an error message.

Parameters
  • line1 (str) – First line of the error message.

  • line2 (str) – Second line of the error message.

New in version 1.9.

run()[source]

Start the event-loop.

simple_menu.TIMEOUT = 256

button_events() timeout marker.

simple_menu.button_events(timeout=None)[source]

Iterate over button presses (event-loop).

This is just a helper function used internally by the menu. But you can of course use it for your own scripts as well. It works like this:

import simple_menu, buttons

for ev in simple_menu.button_events():
    if ev == buttons.BOTTOM_LEFT:
        # Left
        pass
    elif ev == buttons.BOTTOM_RIGHT:
        # Right
        pass
    elif ev == buttons.TOP_RIGHT:
        # Select
        pass

New in version 1.4.

Parameters

timeout (float,optional) –

Timeout after which the generator should yield in any case. If a timeout is defined, the generator will periodically yield simple_menu.TIMEOUT.

New in version 1.9.