button.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536
  1. from typing import Callable, Optional
  2. from ..colors import set_background_color
  3. from ..events import ClickEventArguments, handle_event
  4. from .mixins.disableable_element import DisableableElement
  5. from .mixins.text_element import TextElement
  6. class Button(TextElement, DisableableElement):
  7. def __init__(self,
  8. text: str = '', *,
  9. on_click: Optional[Callable] = None,
  10. color: Optional[str] = 'primary',
  11. ) -> None:
  12. """Button
  13. This element is based on Quasar's `QBtn <https://quasar.dev/vue-components/button>`_ component.
  14. The ``color`` parameter excepts a Quasar color, a Tailwind color, or a CSS color.
  15. If a Quasar color is used, the button will be styled according to the Quasar theme including the color of the text.
  16. Note that there are colors like "red" being both a Quasar color and a CSS color.
  17. In such cases the Quasar color will be used.
  18. :param text: the label of the button
  19. :param on_click: callback which is invoked when button is pressed
  20. :param color: the color of the button (either a Quasar, Tailwind, or CSS color or `None`, default: 'primary')
  21. """
  22. super().__init__(tag='q-btn', text=text)
  23. set_background_color(self, color)
  24. if on_click:
  25. self.on('click', lambda _: handle_event(on_click, ClickEventArguments(sender=self, client=self.client)))
  26. def _text_to_model_text(self, text: str) -> None:
  27. self._props['label'] = text