Jupyter Notebook:如何更改HBox内部Text对象之间的间距?

时间:2016-08-11 21:20:24

标签: python jupyter ipywidgets

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

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)))

它产生以下输出:

enter image description here

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

1 个答案:

答案 0 :(得分:1)

似乎间距是由widget-text类的宽度指定的,宽度为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>')

widgets styled with CSS