How to make a toggle button with niceGUI #509
-
Hi, I’m pretty new to building GUIs and I’m loving NiceGUI so far. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Try something like this from nicegui import ui, events
def update_color(e: events.ValueChangeEventArguments):
e.sender.props(f'color={"green" if e.value else "red"}')
ui.switch(value=True, on_change=update_color).props('keep-color')
ui.run() The keep-color prop ensures that the color is also shown in the disabled state. See https://quasar.dev/vue-components/toggle#introduction. |
Beta Was this translation helpful? Give feedback.
-
If you try to toggle the color of a regular def update_color(e: events.ClickEventArguments):
color = e.sender._props.get('color', 'red')
e.sender.props(f'color={"green" if color == "red" else "red"}')
ui.button('Toggle', on_click=update_color).props('color=green') Here the |
Beta Was this translation helpful? Give feedback.
If you try to toggle the color of a regular
ui.button
, the following example might be helpful:Here the
_props
dictionary is used to read the current color. Normally you would probably store the button state in a separate model variable to avoid reaching into the button's internals.