timer_documentation.py 1.1 KB

12345678910111213141516171819202122232425262728293031
  1. from nicegui import ui
  2. from ..documentation_tools import text_demo
  3. def main_demo() -> None:
  4. from datetime import datetime
  5. label = ui.label()
  6. ui.timer(1.0, lambda: label.set_text(f'{datetime.now():%X}'))
  7. def more() -> None:
  8. @text_demo('Activate, deactivate and cancel a timer', '''
  9. You can activate and deactivate a timer using the `active` property.
  10. You can cancel a timer using the `cancel` method.
  11. After canceling a timer, it cannot be activated anymore.
  12. ''')
  13. def activate_deactivate_demo():
  14. slider = ui.slider(min=0, max=1, value=0.5)
  15. timer = ui.timer(0.1, lambda: slider.set_value((slider.value + 0.01) % 1.0))
  16. ui.switch('active').bind_value_to(timer, 'active')
  17. ui.button('Cancel', on_click=timer.cancel)
  18. @text_demo('Call a function after a delay', '''
  19. You can call a function after a delay using a timer with the `once` parameter.
  20. ''')
  21. def call_after_delay_demo():
  22. def handle_click():
  23. ui.timer(1.0, lambda: ui.notify('Hi!'), once=True)
  24. ui.button('Notify after 1 second', on_click=handle_click)