英文:
Ipywidgets appear separated when displaying them
问题
You can achieve this layout by using the Layout
class from ipywidgets
to adjust the spacing between the widgets. Here's the modified code to place the FloatText
widget next to the checkbox:
import ipywidgets as widgets
from IPython.display import display
label = widgets.Label(value='test:')
checkbox = widgets.Checkbox(indent=False)
float_text = widgets.FloatText()
# Adjust the spacing between the widgets
checkbox.layout.margin = '0 10px 0 0' # Right margin to create space between checkbox and FloatText
widgets_box = widgets.HBox([label, checkbox, float_text])
display(widgets_box)
This code will add some right margin to the checkbox to position the FloatText
widget closer to it.
英文:
I am creating two simple widgets with ipywidget
and when displaying them, I need them to be close to each other. However the default is not showing them like this.
import ipywidgets as widgets
label = widgets.Label(value='test:')
checkbox = widgets.Checkbox(indent=False)
float_text = widgets.FloatText()
widgets_box = widgets.HBox([label, checkbox, float_text])
display(widgets_box)
Why is that? How to i put the Floattext widget just next to the checkbox?
答案1
得分: 0
这是翻译好的部分:
这是因为每个小部件都具有由其内部布局属性设置的预定义的“工厂大小”。小部件的内部布局定义了组件在屏幕上应该有多大。此信息用于HBox
和其他类型的布局管理器,以确定列表中的每个小部件需要多少屏幕空间。因此,可以想象,Checkbox
的默认大小相当大。
解决此问题的一种方法是在创建大型小部件时覆盖预定义的宽度,通过设置具有较小大小的自定义layout
。以下是一个示例:
import ipywidgets as widgets
from IPython.display import display
label = widgets.Label(value='test:', layout={'width': '30px'})
checkbox = widgets.Checkbox(indent=False, layout={'width': '20px'})
float_text = widgets.FloatText()
widgets_box = widgets.HBox([label, checkbox, float_text])
display(widgets_box)
原始实现和使用自定义布局的实现之间的比较:
英文:
That happens because each widget has a predefined "factory size" that is set by its internal layout property. The internal layout of a widget defines how big that component should be on the screen.
This information is used HBox
and the other types of Layout Managers to figure out how much screen space each widget on the list needs. So as you can imagine, the default size of a Checkbox
is quite large.
One solution to this problem is to overwrite the predefined width of large widgets when they are created by setting a custom layout
with a smaller size.
Here is an example:
import ipywidgets as widgets
from IPython.display import display
label = widgets.Label(value='test:', layout={'width': '30px'})
checkbox = widgets.Checkbox(indent=False, layout={'width': '20px'})
float_text = widgets.FloatText()
widgets_box = widgets.HBox([label, checkbox, float_text])
display(widgets_box)
A comparison between the original implementation and one that uses custom layouts:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论