浏览代码

label: add text property to enable binding

Falko Schindler 4 年之前
父节点
当前提交
25d0a5f207
共有 4 个文件被更改,包括 27 次插入14 次删除
  1. 5 3
      main.py
  2. 9 0
      nicegui/elements/element.py
  3. 13 3
      nicegui/elements/label.py
  4. 0 8
      nicegui/elements/value_element.py

+ 5 - 3
main.py

@@ -79,6 +79,8 @@ with ui.row():
             s = ui.switch('c2').bind('value', c, 'value')
         with ui.row():
             model = type('', (), {'value': 1})
-            r1 = ui.radio({1: 'a', 2: 'b', 3: 'c'}).bind('value', model, 'value')
-            r2 = ui.radio({1: 'A', 2: 'B', 3: 'C'}).bind('value', model, 'value')
-            n3 = ui.number().bind('value', model, 'value')
+            ui.radio({1: 'a', 2: 'b', 3: 'c'}).bind('value', model, 'value')
+            ui.radio({1: 'A', 2: 'B', 3: 'C'}).bind('value', model, 'value')
+            with ui.column():
+                ui.number().bind('value', model, 'value')
+                ui.label().bind('text', model, 'value')

+ 9 - 0
nicegui/elements/element.py

@@ -1,4 +1,5 @@
 import justpy as jp
+from ..binding import Binding
 
 class Element:
 
@@ -13,6 +14,8 @@ class Element:
         view.add_page(self.wp)
         self.view = view
 
+        self.bindings = []
+
     def set_classes(self, classes: str):
 
         self.view.classes = classes
@@ -32,3 +35,9 @@ class Element:
 
         self.view.style += ' ' + style
         return self
+
+    def bind(self, attribute, model, model_attribute):
+
+        binding = Binding(self, attribute, model, model_attribute)
+        self.bindings.append(binding)
+        Binding.all_bindings.append(binding)

+ 13 - 3
nicegui/elements/label.py

@@ -1,10 +1,10 @@
 import justpy as jp
-from typing import List
+from typing import Union, List
 from .element import Element
 
 class Label(Element):
 
-    def __init__(self, text: str = '', typography: List[str] = []):
+    def __init__(self, text: str = '', typography: Union[str, List[str]] = []):
 
         if isinstance(typography, str):
             typography = [typography]
@@ -14,6 +14,16 @@ class Label(Element):
 
         super().__init__(view)
 
-    def set_text(self, text: str):
+    @property
+    def text(self):
+
+        return self.view.text
+
+    @text.setter
+    def text(self, text: any):
 
         self.view.text = text
+
+    def set_text(self, text: str):
+
+        self.text = text

+ 0 - 8
nicegui/elements/value_element.py

@@ -2,7 +2,6 @@ import justpy as jp
 from typing import Any, Callable
 import traceback
 from .element import Element
-from ..binding import Binding
 from ..utils import EventArguments
 
 class ValueElement(Element):
@@ -15,7 +14,6 @@ class ValueElement(Element):
 
         super().__init__(view)
 
-        self.bindings = []
         self.on_change = on_change
         self.value = value
 
@@ -46,9 +44,3 @@ class ValueElement(Element):
         for binding in self.bindings:
             if binding.element_attribute == 'value':
                 binding.update_model()
-
-    def bind(self, attribute, model, model_attribute):
-
-        binding = Binding(self, attribute, model, model_attribute)
-        self.bindings.append(binding)
-        Binding.all_bindings.append(binding)