3

假设我在 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)))

它产生以下输出:

在此处输入图像描述

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

4

2 回答 2

2

在 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>')语句作为最后一个语句。

于 2016-08-12T07:14:44.127 回答
2

似乎间距是由于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>')

使用 CSS 样式化的小部件

于 2016-08-12T02:08:07.087 回答