pytermgui.widgets.interactive.toggle
This module contains the Toggle
class.
1"""This module contains the `Toggle` class.""" 2 3from __future__ import annotations 4 5from typing import Any, Callable 6 7from .checkbox import Checkbox 8 9 10class Toggle(Checkbox): 11 """A specialized checkbox showing either of two states""" 12 13 chars = {**Checkbox.chars, **{"delimiter": [" ", " "], "checked": "choose"}} 14 15 def __init__( 16 self, 17 states: tuple[str, str], 18 callback: Callable[[str], Any] | None = None, 19 **attrs: Any, 20 ) -> None: 21 """Initialize object""" 22 23 self.states = states 24 25 self.set_char("checked", states[0]) 26 self.set_char("unchecked", states[1]) 27 28 super().__init__(callback, **attrs) 29 self.toggle(run_callback=False) 30 31 def _run_callback(self) -> None: 32 """Run the toggle callback with the label as its argument""" 33 34 if self.callback is not None: 35 self.callback(self.label)
11class Toggle(Checkbox): 12 """A specialized checkbox showing either of two states""" 13 14 chars = {**Checkbox.chars, **{"delimiter": [" ", " "], "checked": "choose"}} 15 16 def __init__( 17 self, 18 states: tuple[str, str], 19 callback: Callable[[str], Any] | None = None, 20 **attrs: Any, 21 ) -> None: 22 """Initialize object""" 23 24 self.states = states 25 26 self.set_char("checked", states[0]) 27 self.set_char("unchecked", states[1]) 28 29 super().__init__(callback, **attrs) 30 self.toggle(run_callback=False) 31 32 def _run_callback(self) -> None: 33 """Run the toggle callback with the label as its argument""" 34 35 if self.callback is not None: 36 self.callback(self.label)
A specialized checkbox showing either of two states
Toggle( states: tuple[str, str], callback: Optional[Callable[[str], Any]] = None, **attrs: Any)
16 def __init__( 17 self, 18 states: tuple[str, str], 19 callback: Callable[[str], Any] | None = None, 20 **attrs: Any, 21 ) -> None: 22 """Initialize object""" 23 24 self.states = states 25 26 self.set_char("checked", states[0]) 27 self.set_char("unchecked", states[1]) 28 29 super().__init__(callback, **attrs) 30 self.toggle(run_callback=False)
Initialize object
chars: dict[str, typing.Union[typing.List[str], str]] = {'delimiter': [' ', ' '], 'checked': 'choose', 'unchecked': ' '}
Default characters for this class