1
0

switch_documentation.py 880 B

1234567891011121314151617181920212223242526
  1. from nicegui import ui
  2. from . import doc
  3. @doc.demo(ui.switch)
  4. def main_demo() -> None:
  5. switch = ui.switch('switch me')
  6. ui.label('Switch!').bind_visibility_from(switch, 'value')
  7. @doc.demo('Handle User Interaction', '''
  8. The `on_change` function passed via parameter will be called when the switch is clicked
  9. *and* when the value changes via `set_value` call.
  10. To execute a function only when the user interacts with the switch, you can use the generic `on` method.
  11. ''')
  12. def user_interaction():
  13. with ui.row():
  14. s1 = ui.switch(on_change=lambda e: ui.notify(str(e.value)))
  15. ui.button('set value', on_click=lambda: s1.set_value(not s1.value))
  16. with ui.row():
  17. s2 = ui.switch().on('click', lambda e: ui.notify(str(e.sender.value)))
  18. ui.button('set value', on_click=lambda: s2.set_value(not s2.value))
  19. doc.reference(ui.switch)