button.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import asyncio
  2. from typing import Any, Callable, Optional
  3. from ..events import ClickEventArguments, handle_event
  4. from .mixins.color_elements import BackgroundColorElement
  5. from .mixins.disableable_element import DisableableElement
  6. from .mixins.text_element import TextElement
  7. class Button(TextElement, DisableableElement, BackgroundColorElement):
  8. def __init__(self,
  9. text: str = '', *,
  10. on_click: Optional[Callable[..., Any]] = None,
  11. color: Optional[str] = 'primary',
  12. icon: Optional[str] = None,
  13. ) -> None:
  14. """Button
  15. This element is based on Quasar's `QBtn <https://quasar.dev/vue-components/button>`_ component.
  16. The ``color`` parameter accepts a Quasar color, a Tailwind color, or a CSS color.
  17. If a Quasar color is used, the button will be styled according to the Quasar theme including the color of the text.
  18. Note that there are colors like "red" being both a Quasar color and a CSS color.
  19. In such cases the Quasar color will be used.
  20. :param text: the label of the button
  21. :param on_click: callback which is invoked when button is pressed
  22. :param color: the color of the button (either a Quasar, Tailwind, or CSS color or `None`, default: 'primary')
  23. :param icon: the name of an icon to be displayed on the button (default: `None`)
  24. """
  25. super().__init__(tag='q-btn', text=text, background_color=color)
  26. if icon:
  27. self._props['icon'] = icon
  28. if on_click:
  29. self.on('click', lambda _: handle_event(on_click, ClickEventArguments(sender=self, client=self.client)), [])
  30. def _text_to_model_text(self, text: str) -> None:
  31. self._props['label'] = text
  32. async def clicked(self) -> None:
  33. """Wait until the button is clicked."""
  34. event = asyncio.Event()
  35. self.on('click', event.set, [])
  36. await self.client.connected()
  37. await event.wait()