Ver Fonte

fix text binding

Falko Schindler há 2 anos atrás
pai
commit
9e144b6a4b
2 ficheiros alterados com 9 adições e 20 exclusões
  1. 7 13
      nicegui/binding.py
  2. 2 7
      nicegui/elements/text_element.py

+ 7 - 13
nicegui/binding.py

@@ -1,7 +1,6 @@
 import asyncio
 import logging
 import time
-from abc import ABC, abstractmethod
 from collections import defaultdict
 from typing import Any, Callable, Optional, Set, Tuple
 
@@ -44,12 +43,14 @@ def bind_to(self_obj: Any, self_name: str, other_obj: Any, other_name: str, forw
     bindings[(id(self_obj), self_name)].append((self_obj, other_obj, other_name, forward))
     if (id(self_obj), self_name) not in bindable_properties:
         active_links.append((self_obj, self_name, other_obj, other_name, forward))
+    propagate(self_obj, self_name)
 
 
 def bind_from(self_obj: Any, self_name: str, other_obj: Any, other_name: str, backward: Callable) -> None:
     bindings[(id(other_obj), other_name)].append((other_obj, self_obj, self_name, backward))
     if (id(other_obj), other_name) not in bindable_properties:
         active_links.append((other_obj, other_name, self_obj, self_name, backward))
+    propagate(other_obj, other_name)
 
 
 def bind(self_obj: Any, self_name: str, other_obj: Any, other_name: str, *,
@@ -80,11 +81,11 @@ class BindableProperty:
             self.on_change(owner, value)
 
 
-class BindTextMixin(ABC):
+class BindTextMixin:
     """
     Mixin providing bind methods for attribute text.
     """
-    text = BindableProperty()
+    text = BindableProperty(on_change=lambda sender, text: sender.on_text_change(text))
 
     def bind_text_to(self, target_object: Any, target_name: str, forward: Callable = lambda x: x):
         bind_to(self, 'text', target_object, target_name, forward)
@@ -99,19 +100,12 @@ class BindTextMixin(ABC):
         bind(self, 'text', target_object, target_name, forward=forward, backward=backward)
         return self
 
-    @property
-    @abstractmethod
-    def text(self) -> str:
-        pass
-
-    @text.setter
-    @abstractmethod
-    def text(self, text: str) -> None:
-        pass
-
     def set_text(self, text: str) -> None:
         self.text = text
 
+    def on_text_change(self, text: str) -> None:
+        pass
+
 
 class BindValueMixin:
     """

+ 2 - 7
nicegui/elements/text_element.py

@@ -8,12 +8,7 @@ class TextElement(Element, BindTextMixin):
     def __init__(self, tag: str, text: str) -> None:
         super().__init__(tag)
         self.text = text
+        self.bind_text_to(self, '_text')
 
-    @property
-    def text(self) -> str:
-        return self._text
-
-    @text.setter
-    def text(self, text: str) -> None:
-        self._text = text
+    def on_text_change(self, _) -> None:
         self.update()