简体   繁体   中英

Python ipywidgets: Setting different colors for Button

The documentation explains the basic colors you can set on the button.

widgets.Button(
    description='Click me',
    disabled=False,
    button_style='', # 'success', 'info', 'warning', 'danger' or ''
    tooltip='Click me',
    icon='check'
)

Are there additional colors that can be used? How?

Documentation explains that widgets have a style attribute that has different properties depending on the widget type.

In our case

import ipywidgets as widgets
b=widgets.Button(description=button_style, button_style='')
b.style.keys

['_model_module',
 '_model_module_version',
 '_model_name',
 '_view_count',
 '_view_module',
 '_view_module_version',
 '_view_name',
 'button_color',
 'font_weight']

So, apart from the basic colors

import ipywidgets as widgets
for button_style in ['primary','success', 'info', 'warning', 'danger' , '']:
  print(button_style)
  widgets.Button(description=button_style, button_style=button_style)

在此处输入图像描述

You can have any of the html colors

name_colors="GreenYellow Chartreuse LawnGreen Lime LimeGreen".split() 
for button_color in name_colors:
  print(button_color)
  b=widgets.Button(description=button_style, button_style='')
  b.style.button_color = button_color
  b

在此处输入图像描述


This may be needed in your case to display multiple outputs

from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM