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

2024-06-16 12:08:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在创建一个带有文本的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)

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


Tags: 代码from文本importasipythondisplaybutton
1条回答
网友
1楼 · 发布于 2024-06-16 12:08:31

您可以创建一个Layout类的对象,并将其用作不同窗口小部件的宽度/高度样式的属性。你可以阅读更多关于它的here。在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)

enter image description here

让我们增加description长度:

^{pr2}$

enter image description here

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

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

相关问题 更多 >