如何在ipywidget按钮中显示全文?

时间:2019-03-05 07:05:52

标签: python ipywidgets

我正在创建带有一些文本的ipywidget按钮。但全文未显示在按钮中:

enter image description here

我使用的代码如下:

import ipywidgets as widgets
from IPython.display import display
button = widgets.Button(
    description='Test button with some text.',
    disabled=False,
    display='flex',
    flex_flow='column',
    align_items='stretch'
)        
display(button)

使用什么选项可以在按钮上显示全文(即增加按钮的宽度以显示全文)?

2 个答案:

答案 0 :(得分:1)

您可以创建Layout类的对象,并将其用作为不同小部件设置宽度/高度样式的属性。您可以详细了解hereLayout中定义的属性是CSS属性。因此,要使文本适合按钮的宽度,只需设置width='auto'

import ipywidgets as widgets
from IPython.display import display

layout = widgets.Layout(width='auto', height='40px') #set width and height

button = widgets.Button(
    description='Test button with some text.',
    disabled=False,
    display='flex',
    flex_flow='column',
    align_items='stretch', 
    layout = layout
)        
display(button)

enter image description here

让我们增加description的长度:

description='Test button with some text and some more'

enter image description here

您也可以在其他小部件上重新使用layout属性:

widgets.Button(description='Another button with the same layout', layout=button.layout)

答案 1 :(得分:0)

添加style= {'description_width': 'initial'}

button = widgets.Button(
    description='Test button with some text.',
    disabled=False,
    display='flex',
    flex_flow='column',
    align_items='stretch', 
    style= {'description_width': 'initial'}
)