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