label.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import justpy as jp
  2. from ..binding import BindableProperty, bind_from, bind_to
  3. from .element import Element
  4. class Label(Element):
  5. text = BindableProperty()
  6. def __init__(self, text: str = ''):
  7. """Label
  8. Displays some text.
  9. :param text: the content of the label
  10. """
  11. view = jp.Div(text=text, temp=False)
  12. super().__init__(view)
  13. self.text = text
  14. self.bind_text_to(self.view, 'text')
  15. def set_text(self, text: str):
  16. self.text = text
  17. def bind_text_to(self, target_object, target_name, forward=lambda x: x):
  18. bind_to(self, 'text', target_object, target_name, forward=forward)
  19. return self
  20. def bind_text_from(self, target_object, target_name, backward=lambda x: x):
  21. bind_from(self, 'text', target_object, target_name, backward=backward)
  22. return self
  23. def bind_text(self, target_object, target_name, forward=lambda x: x, backward=lambda x: x):
  24. bind_from(self, 'text', target_object, target_name, backward=backward)
  25. bind_to(self, 'text', target_object, target_name, forward=forward)
  26. return self