Jupyter Notebook:如何更改 HBox 中文本对象之间的间距?

Jupyter Notebook: How to change spacing between Text objects inside HBox?

假设我在 Jupyter Notebook 的单元格中有以下代码:

from ipywidgets.widgets import HBox, Text
from IPython.display import display

text1 = Text(description = "text1", width = 100)
text2 = Text(description = "text2", width = 100)
text3 = Text(description = "text3", width = 100)
text4 = Text(description = "text4", width = 100)
text5 = Text(description = "text5", width = 100)
display(HBox((text1, text2, text3, text4, text5)))

它产生以下输出:

如您所见,文本对象之间存在大量不必要的间距。如何更改间距以使它们保持在单元格内?

似乎间距是由于 widget-text class 的宽度被指定为 350 像素的宽度。尝试导入 HTML 模块(即 from IPython.display import display, HTML)并将 HTML('<style> .widget-text { width: auto; } </style>') 插入单元格。

你应该得到这样的结果:

from ipywidgets.widgets import HBox, Text
from IPython.display import display, HTML

text1 = Text(description = "text1", width = 100)
text2 = Text(description = "text2", width = 100)
text3 = Text(description = "text3", width = 100)
text4 = Text(description = "text4", width = 100)
text5 = Text(description = "text5", width = 100)

display(HBox((text1, text2, text3, text4, text5)))
HTML('<style> .widget-text { width: auto; } </style>')

根据 Zarak 的回答,您可以这样做

from ipywidgets.widgets import HBox, Text
from IPython.display import display, HTML

text1 = Text(description = "text1", width = 100)
text2 = Text(description = "text2", width = 100)
text3 = Text(description = "text3", width = 100)
text4 = Text(description = "text4", width = 100)
text5 = Text(description = "text5", width = 100)

display(HBox((text1, text2, text3, text4, text5)))
display(HTML('<style> .widget-text { width: auto; } </style>'))
print('hi')

这样,您就不需要将 HTML('<style> .widget-text { width: auto; } </style>') 语句作为最后一个语句。