Pārlūkot izejas kodu

improve binding example

Falko Schindler 4 gadi atpakaļ
vecāks
revīzija
fdce30d8e6
2 mainītis faili ar 20 papildinājumiem un 11 dzēšanām
  1. 14 11
      main.py
  2. 6 0
      nicegui/elements/number.py

+ 14 - 11
main.py

@@ -4,17 +4,6 @@ from datetime import datetime
 from matplotlib import pyplot as plt
 from matplotlib import pyplot as plt
 import numpy as np
 import numpy as np
 
 
-with ui.card(), ui.row():
-    ui.label('Binding', 'h5')
-    n1 = ui.number(value=0.5, format='%.2f', on_change=lambda e: print(e.value))
-    n2 = ui.number(format='%.3f').bind('value', n1, 'value')
-
-    c1 = ui.checkbox('c1', value=True, on_change=lambda e: print(e.value))
-    s1 = ui.switch('c2', value=True, on_change=lambda e: print(e.value))
-
-    c2 = ui.checkbox('c2', on_change=lambda e: print(e.value))
-    s2 = ui.switch('s2', on_change=lambda e: print(e.value)).bind('value', c2, 'value')
-
 with ui.row():
 with ui.row():
     with ui.card():
     with ui.card():
         ui.label('Interactive elements', 'h5')
         ui.label('Interactive elements', 'h5')
@@ -79,3 +68,17 @@ with ui.row():
             [np.sin(datetime.now().timestamp()) + 0.02 * np.random.randn()],
             [np.sin(datetime.now().timestamp()) + 0.02 * np.random.randn()],
             [np.cos(datetime.now().timestamp()) + 0.02 * np.random.randn()],
             [np.cos(datetime.now().timestamp()) + 0.02 * np.random.randn()],
         ]))
         ]))
+
+    with ui.card():
+        ui.label('Binding', 'h5')
+        with ui.row():
+            n1 = ui.number(value=1.2345, format='%.2f')
+            n2 = ui.number(format='%.3f').bind('value', n1, 'value')
+        with ui.row():
+            c = ui.checkbox('c1')
+            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')

+ 6 - 0
nicegui/elements/number.py

@@ -22,3 +22,9 @@ class Number(FloatElement):
         )
         )
 
 
         super().__init__(view, value=value, format=format, on_change=on_change)
         super().__init__(view, value=value, format=format, on_change=on_change)
+
+    def handle_change(self, msg):
+
+        msg['value'] = float(msg['value'])
+
+        return super().handle_change(msg)