如何在 ipywidget 按钮中显示完整文本?

20

我正在创建一个带有一些文本的 ipywidget 按钮。 但是按钮中没有显示完整的文本:

输入图像描述

我使用的代码如下:

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个回答

18

您可以创建Layout类的一个对象,并将其用作属性来为不同的小部件设置宽度/高度样式。您可以在此处了解更多信息。定义在Layout中的属性是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)

在此输入图片描述

让我们增加说明的长度:

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

您也可以在其他小部件上重复使用布局属性:

在此输入图片描述

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

但现在我两边都有额外的空间。理想情况下,我希望文本填满空间,即文本长度多少就有多少空间。 - Alex
好的,我刚刚编辑了我的答案。width 应该设置为 auto - amanb

14

添加 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'}
)        

尝试使用 width: initial - xemexpress

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接