Change color of ipywidget color upon click (active)

I have the following button, in which I would like to turn green when clicked:

see_button = widgets.Button(
description=‘Click me’,
disabled=False,
button_style=‘’, # ‘primary’, ‘info’, ‘warning’, ‘danger’ or ‘’
style=dict(font_style=‘italic’,
button_color=‘DarkGray’,description_width= ‘200px’),
center = ‘right’
)
display(see_button)

There is no option to under button_style or button color to change upon clicking. Also you like it to turn back to grey if unclicked or another button is clicked.

You can use the on_click method to run another function.

Here when the button is clicked it changes to red:

import ipywidgets as widgets

button = widgets.Button(
    description='click_me',
    button_style='info',
)

def change_color(b):
    button.button_style = 'danger'

button.on_click(change_color)

button

You can then add more logic to get the desired behaviors you were describing.

2 Likes

Thank you, that worked perfectly

2 Likes