Search code examples
pythonbuttonipywidgets

python ipywidgets button color


When using ipywidgets the way to create a button is basically as follows:

import ipywidgets as widgets
layout_btn = widgets.Layout(width='200px')
mybtn = widgets.Button(description='load',
                       disabled=False,
                       button_style='', # 'success', 'info', 'warning', 'danger' or ''
                       tooltip='this will happen:...',
                       icon='',
                        layout=layout_btn)

in the Layout object several things can be defined: see here: https://ipywidgets.readthedocs.io/en/latest/examples/Widget%20Styling.html

Within all the layout possibilities I do not see the possibility of modifying the color of the button itself.

The question is how to make a button of a particular color (RBG or HEX)?

Thanks.


Solution

  • Possible solution is the following:

    import ipywidgets as widgets
    
    layout_btn = widgets.Layout(width='200px')
    mybtn = widgets.Button(description='load',
                           disabled=False,
                           button_style='', # 'success', 'info', 'warning', 'danger' or ''
                           tooltip='this will happen:...',
                           icon='',
                           layout=layout_btn)
    
    # apply background color to the button
    mybtn.style.button_color = '#90ee90' # or 'lightgreen' or 'rgb(144,238,144)'
    
    mybtn
    

    Returns

    enter image description here

    Follow the link for more details about the style attribute.