checkbox_documentation.py 896 B

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