Skip to content

How to make a toggle button with niceGUI #509

Closed Answered by falkoschindler
lucasrivas12 asked this question in Q&A
Discussion options

You must be logged in to vote

If you try to toggle the color of a regular ui.button, the following example might be helpful:

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 _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.

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by falkoschindler
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants