Skip to content

toggle

This module contains the Toggle class.

Toggle

Bases: Checkbox

A specialized checkbox showing either of two states

Source code in pytermgui/widgets/toggle.py
10
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
class Toggle(Checkbox):
    """A specialized checkbox showing either of two states"""

    chars = {**Checkbox.chars, **{"delimiter": [" ", " "], "checked": "choose"}}

    def __init__(
        self,
        states: tuple[str, str],
        callback: Callable[[str], Any] | None = None,
        **attrs: Any,
    ) -> None:
        """Initialize object"""

        self.states = states

        self.set_char("checked", states[0])
        self.set_char("unchecked", states[1])

        super().__init__(callback, **attrs)
        self.toggle(run_callback=False)

    def _run_callback(self) -> None:
        """Run the toggle callback with the label as its argument"""

        if self.callback is not None:
            self.callback(self.label)

__init__(states, callback=None, attrs)

Initialize object

Source code in pytermgui/widgets/toggle.py
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
def __init__(
    self,
    states: tuple[str, str],
    callback: Callable[[str], Any] | None = None,
    **attrs: Any,
) -> None:
    """Initialize object"""

    self.states = states

    self.set_char("checked", states[0])
    self.set_char("unchecked", states[1])

    super().__init__(callback, **attrs)
    self.toggle(run_callback=False)