浏览代码

Merge pull request #849 from zauberzeug/awaitable_button

Await button click
Falko Schindler 2 年之前
父节点
当前提交
02f9dab1d7
共有 2 个文件被更改,包括 26 次插入0 次删除
  1. 8 0
      nicegui/elements/button.py
  2. 18 0
      website/more_documentation/button_documentation.py

+ 8 - 0
nicegui/elements/button.py

@@ -1,3 +1,4 @@
+import asyncio
 from typing import Callable, Optional
 
 from ..colors import set_background_color
@@ -34,3 +35,10 @@ class Button(TextElement, DisableableElement):
 
     def _text_to_model_text(self, text: str) -> None:
         self._props['label'] = text
+
+    async def clicked(self) -> None:
+        """Wait until the button is clicked."""
+        event = asyncio.Event()
+        self.on('click', event.set)
+        await self.client.connected()
+        await event.wait()

+ 18 - 0
website/more_documentation/button_documentation.py

@@ -1,5 +1,23 @@
 from nicegui import ui
 
+from ..documentation_tools import text_demo
+
 
 def main_demo() -> None:
     ui.button('Click me!', on_click=lambda: ui.notify(f'You clicked me!'))
+
+
+def more() -> None:
+    @text_demo('Await button click', '''
+        Sometimes it is convenient to wait for a button click before continuing the execution.
+    ''')
+    async def await_button_click() -> None:
+        # @ui.page('/')
+        # async def index():
+            b = ui.button('Step')
+            await b.clicked()
+            ui.label('One')
+            await b.clicked()
+            ui.label('Two')
+            await b.clicked()
+            ui.label('Three')