Skip to content

Component

Component is the central class of interactive-buttons. It wraps a list of Button instances and provides three layout methods that render the buttons in the terminal, capture keyboard input, and return the selected value.


Class reference

Component

Bases: MatrixButtons, LinearButtons, ColumnButtons

Interactive button component combining all layout modes.

Use column_buttons, linear_buttons, or matrix_buttons to render the buttons and get the user's selection.

Parameters:

Name Type Description Default
buttons list[Button]

Ordered list of Button instances.

required
global_buttons_style ButtonStyle

Style applied to buttons without a local override.

ButtonStyle(**DEFAULT_STYLE)
display_index bool

Prefix each button with a digit keys shortcut when True.

False
index_begin int

Digit assigned to the first button when display_index is active. Defaults to 1.

1
auto_erase bool

Erase the rendered output automatically after selection. Defaults to False.

False

Raises:

Type Description
MoreButtonsThanIndexes

When display_index is True and buttons exceed available digits.

KeyBindError

On key-bind/display-index conflict or duplicate keys across buttons.

column_buttons

column_buttons() -> Any

Display buttons in a vertical column and return the selected value.

Renders all buttons stacked vertically. The user navigates with the UP/DOWN arrow keys and confirms with ENTER. When display_index is enabled, pressing the corresponding digit jumps to that button, otherwise configured key-bindings can be used (butr not both at the same time).

Returns:

Type Description
Any

The value of the button confirmed by the user, this value can be anything.

linear_buttons

linear_buttons() -> Any

Display buttons in a horizontal row and return the selected value.

Renders all buttons side by side on a single line. The user navigates with LEFT/RIGHT arrow keys and confirms with ENTER. When display_index is enabled, pressing the corresponding digit jumps to that button, otherwise configured key-bindings can be used (butr not both at the same time).

Returns:

Type Description
Any

The value of the button confirmed by the user, this value can be anything.

matrix_buttons

matrix_buttons() -> Any

Display buttons in a square grid and return the selected value.

Arranges buttons in a square grid of side length. The user navigates with arrow keys and confirms with ENTER. When display_index is enabled, pressing the corresponding digit jumps to that button, otherwise configured key-bindings can be used (butr not both at the same time).

Returns:

Type Description
Any

The value of the button confirmed by the user, this value can be anything.

erase_buttons

erase_buttons(bypass: bool = False) -> None

Erase the rendered buttons from the terminal.

No-op when auto_erase is enabled, unless bypass is True which forces erasure and silences the warning.

Parameters:

Name Type Description Default
bypass bool

Force erasure even when auto_erase is enabled. Defaults to False.

False

Relationships

Component
+-- inherits -> LinearButtons   (provides linear_buttons)
+-- inherits -> ColumnButtons   (provides column_buttons)
+-- inherits -> MatrixButtons   (provides matrix_buttons)

All three layout classes share the same ButtonsKindHost protocol - Component satisfies it through its __init__.


Error conditions

Exception When
MoreButtonsThanIndexes display_index=True and buttons exceed 10 - index_begin
KeyBindError key_bind used together with display_index, or duplicate keys
EmulatedTerminalDetected Terminal cannot provide raw keystroke input