button.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. from typing import Callable
  2. import justpy as jp
  3. from .element import Element
  4. from ..utils import handle_exceptions, provide_arguments
  5. class Button(Element):
  6. def __init__(self,
  7. text: str = '',
  8. *,
  9. on_click: Callable = None,
  10. ):
  11. """Button Element
  12. :param text: the label of the button
  13. :param on_click: callback which is invoked when button is pressed
  14. """
  15. view = jp.QButton(label=text, color='primary')
  16. if on_click is not None:
  17. view.on('click', handle_exceptions(provide_arguments(on_click)))
  18. super().__init__(view)
  19. @property
  20. def text(self):
  21. return self.view.label
  22. @text.setter
  23. def text(self, text: any):
  24. self.view.label = text
  25. def set_text(self, text: str):
  26. self.text = text
  27. def bind_text_to(self, target, forward=lambda x: x):
  28. self.text.bind_to(target, forward=forward, nesting=1)
  29. return self
  30. def bind_text_from(self, target, backward=lambda x: x):
  31. self.text.bind_from(target, backward=backward, nesting=1)
  32. return self
  33. def bind_text(self, target, forward=lambda x: x, backward=lambda x: x):
  34. self.text.bind(target, forward=forward, backward=backward, nesting=1)
  35. return self