Skip to content

keyboard_button

This module provides a keyboard-accessible button.

KeyboardButton

Bases: Button

A button with keyboard mnemonics in mind.

Shoutout to the HackerNews thread where this was originally suggested

https://news.ycombinator.com/item?id=30517299#30533444

Source code in pytermgui/widgets/keyboard_button.py
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
class KeyboardButton(Button):
    """A button with keyboard mnemonics in mind.

    Shoutout to the HackerNews thread where this was originally suggested:
        https://news.ycombinator.com/item?id=30517299#30533444
    """

    chars = {**Button.chars, **{"bracket": ["(", ")"]}}

    is_bindable = True

    def __init__(
        self,
        label: str,
        onclick: Callable[[Button], Any],
        index: int = 0,
        bound: str | None = None,
    ) -> None:
        """Initializes a KeyboardButton.

        For example, `KeyboardButton("Help")` will look like: "[ (H)elp ]", and
        `KeyboardButton("Test", index=1)` will give "[ T(e)st ]"

        Args:
            label: The label of the button.
            onclick: The callback to be executed when the button is activated.
            index: The index of the label to use as the binding character.
            bound: The keybind that activates this button. Defaults to `keys.CTRL_{char}`
                is used as the default binding.
        """

        if bound is None:
            bound = getattr(keys, "CTRL_" + label[index].upper())

        brackets = "{}".join(self._get_char("bracket"))
        original = label
        label = label[:index] + brackets.format(label[index])

        if index > -1:
            label += original[index + 1 :]

        super().__init__(label, onclick)
        self.bind(bound, lambda btn, _: onclick(btn))

__init__(label, onclick, index=0, bound=None)

Initializes a KeyboardButton.

For example, KeyboardButton("Help") will look like: "[ (H)elp ]", and KeyboardButton("Test", index=1) will give "[ T(e)st ]"

Parameters:

Name Type Description Default
label str

The label of the button.

required
onclick Callable[[Button], Any]

The callback to be executed when the button is activated.

required
index int

The index of the label to use as the binding character.

0
bound str | None

The keybind that activates this button. Defaults to keys.CTRL_{char} is used as the default binding.

None
Source code in pytermgui/widgets/keyboard_button.py
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
def __init__(
    self,
    label: str,
    onclick: Callable[[Button], Any],
    index: int = 0,
    bound: str | None = None,
) -> None:
    """Initializes a KeyboardButton.

    For example, `KeyboardButton("Help")` will look like: "[ (H)elp ]", and
    `KeyboardButton("Test", index=1)` will give "[ T(e)st ]"

    Args:
        label: The label of the button.
        onclick: The callback to be executed when the button is activated.
        index: The index of the label to use as the binding character.
        bound: The keybind that activates this button. Defaults to `keys.CTRL_{char}`
            is used as the default binding.
    """

    if bound is None:
        bound = getattr(keys, "CTRL_" + label[index].upper())

    brackets = "{}".join(self._get_char("bracket"))
    original = label
    label = label[:index] + brackets.format(label[index])

    if index > -1:
        label += original[index + 1 :]

    super().__init__(label, onclick)
    self.bind(bound, lambda btn, _: onclick(btn))