toggle.py 1.2 KB

1234567891011121314151617181920212223242526272829
  1. from typing import Any, Callable, Dict, List, Optional, Union
  2. from .choice_element import ChoiceElement
  3. from .mixins.disableable_element import DisableableElement
  4. class Toggle(ChoiceElement, DisableableElement):
  5. def __init__(self,
  6. options: Union[List, Dict], *,
  7. value: Any = None,
  8. on_change: Optional[Callable[..., Any]] = None,
  9. ) -> None:
  10. """Toggle
  11. The options can be specified as a list of values, or as a dictionary mapping values to labels.
  12. After manipulating the options, call `update()` to update the options in the UI.
  13. :param options: a list ['value1', ...] or dictionary `{'value1':'label1', ...}` specifying the options
  14. :param value: the initial value
  15. :param on_change: callback to execute when selection changes
  16. """
  17. super().__init__(tag='q-btn-toggle', options=options, value=value, on_change=on_change)
  18. def _msg_to_value(self, msg: Dict) -> Any:
  19. return self._values[msg['args']]
  20. def _value_to_model_value(self, value: Any) -> Any:
  21. return self._values.index(value) if value in self._values else None