switch.py 752 B

123456789101112131415161718192021
  1. from typing import Awaitable, Callable, Optional, Union
  2. import justpy as jp
  3. from .bool_element import BoolElement
  4. class Switch(BoolElement):
  5. def __init__(self,
  6. text: str = '',
  7. *,
  8. value: bool = False,
  9. on_change: Optional[Union[Callable, Awaitable]] = None,
  10. ):
  11. """Switch Element
  12. :param text: the label to display next to the switch
  13. :param value: set to `True` if initally it should be active; default is `False`
  14. :param on_click: callback which is invoked when state is changed by the user
  15. """
  16. view = jp.QToggle(text=text, input=self.handle_change)
  17. super().__init__(view, value=value, on_change=on_change)