浏览代码

trying to update all childs rcursively to update timer

Rodja Trappe 4 年之前
父节点
当前提交
4e48b8894b
共有 2 个文件被更改,包括 13 次插入2 次删除
  1. 2 1
      main.py
  2. 11 1
      nice_gui.py

+ 2 - 1
main.py

@@ -15,9 +15,10 @@ with ui.card():
             ui.label('C2')
             ui.label('C3')
 
-ui.run()
 
 with ui.row():
     ui.label('Time:')
     time = ui.label()
     ui.timer(0.1, lambda: time.set_text(datetime.now().strftime("%X")))
+
+ui.run()

+ 11 - 1
nice_gui.py

@@ -6,6 +6,9 @@ import inspect
 import time
 import asyncio
 from utils import handle_exceptions, provide_arguments
+import icecream
+
+icecream.install()
 
 wp = jp.WebPage(delete_flag=False, title='Nice GUI', favicon='favicon.png')
 main = jp.Div(a=wp, classes='m-4 flex flex-col items-start gap-4')
@@ -70,12 +73,19 @@ class Ui(Starlette):
 
     def timer(self, inverval, callback):
 
+        def update(view):
+            if view.components:
+                for v in view.components:
+                    update(v)
+
+            jp.run_task(view.update())
+
         async def loop():
 
             while True:
                 start = time.time()
                 handle_exceptions(callback)()
-                jp.run_task(wp.update())
+                update(view_stack[-1])
                 dt = time.time() - start
                 await asyncio.sleep(inverval - dt)