pytermgui.widgets.keyboard_button
This module provides a keyboard-accessible button.
1"""This module provides a keyboard-accessible button.""" 2 3from __future__ import annotations 4 5from typing import Any, Callable 6 7from ..input import keys 8from .button import Button 9 10 11class KeyboardButton(Button): 12 """A button with keyboard mnemonics in mind. 13 14 Shoutout to the HackerNews thread where this was originally suggested: 15 https://news.ycombinator.com/item?id=30517299#30533444 16 """ 17 18 chars = {**Button.chars, **{"bracket": ["(", ")"]}} 19 20 is_bindable = True 21 22 def __init__( 23 self, 24 label: str, 25 onclick: Callable[[Button], Any], 26 index: int = 0, 27 bound: str | None = None, 28 ) -> None: 29 """Initializes a KeyboardButton. 30 31 For example, `KeyboardButton("Help")` will look like: "[ (H)elp ]", and 32 `KeyboardButton("Test", index=1)` will give "[ T(e)st ]" 33 34 Args: 35 label: The label of the button. 36 char: The "highlighted" character inside the label. Defaults to first 37 character of the label. 38 bound: The keybind that activates this button. Defaults to CTRL_{char} 39 is used as the default binding. 40 """ 41 42 if bound is None: 43 bound = getattr(keys, "CTRL_" + label[index].upper()) 44 45 brackets = "{}".join(self._get_char("bracket")) 46 label = label[:index] + brackets.format(label[index]) 47 48 if index > -1: 49 label += label[index + 1 :] 50 51 super().__init__(label, onclick) 52 self.bind(bound, lambda btn, _: onclick(btn))
12class KeyboardButton(Button): 13 """A button with keyboard mnemonics in mind. 14 15 Shoutout to the HackerNews thread where this was originally suggested: 16 https://news.ycombinator.com/item?id=30517299#30533444 17 """ 18 19 chars = {**Button.chars, **{"bracket": ["(", ")"]}} 20 21 is_bindable = True 22 23 def __init__( 24 self, 25 label: str, 26 onclick: Callable[[Button], Any], 27 index: int = 0, 28 bound: str | None = None, 29 ) -> None: 30 """Initializes a KeyboardButton. 31 32 For example, `KeyboardButton("Help")` will look like: "[ (H)elp ]", and 33 `KeyboardButton("Test", index=1)` will give "[ T(e)st ]" 34 35 Args: 36 label: The label of the button. 37 char: The "highlighted" character inside the label. Defaults to first 38 character of the label. 39 bound: The keybind that activates this button. Defaults to CTRL_{char} 40 is used as the default binding. 41 """ 42 43 if bound is None: 44 bound = getattr(keys, "CTRL_" + label[index].upper()) 45 46 brackets = "{}".join(self._get_char("bracket")) 47 label = label[:index] + brackets.format(label[index]) 48 49 if index > -1: 50 label += label[index + 1 :] 51 52 super().__init__(label, onclick) 53 self.bind(bound, lambda btn, _: onclick(btn))
A button with keyboard mnemonics in mind.
Shoutout to the HackerNews thread where this was originally suggested
KeyboardButton( label: str, onclick: Callable[[pytermgui.widgets.button.Button], Any], index: int = 0, bound: str | None = None)
23 def __init__( 24 self, 25 label: str, 26 onclick: Callable[[Button], Any], 27 index: int = 0, 28 bound: str | None = None, 29 ) -> None: 30 """Initializes a KeyboardButton. 31 32 For example, `KeyboardButton("Help")` will look like: "[ (H)elp ]", and 33 `KeyboardButton("Test", index=1)` will give "[ T(e)st ]" 34 35 Args: 36 label: The label of the button. 37 char: The "highlighted" character inside the label. Defaults to first 38 character of the label. 39 bound: The keybind that activates this button. Defaults to CTRL_{char} 40 is used as the default binding. 41 """ 42 43 if bound is None: 44 bound = getattr(keys, "CTRL_" + label[index].upper()) 45 46 brackets = "{}".join(self._get_char("bracket")) 47 label = label[:index] + brackets.format(label[index]) 48 49 if index > -1: 50 label += label[index + 1 :] 51 52 super().__init__(label, onclick) 53 self.bind(bound, lambda btn, _: onclick(btn))
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.
- char: The "highlighted" character inside the label. Defaults to first character of the label.
- bound: The keybind that activates this button. Defaults to CTRL_{char} is used as the default binding.
chars: dict[str, typing.Union[typing.List[str], str]] = {'delimiter': ['[ ', ' ]'], 'bracket': ['(', ')']}
Default characters for this class