【问题标题】:Change the alignment of the content of a Label in Ipywidget在 Ipywidget 中更改 Label 内容的对齐方式
【发布时间】:2019-10-16 15:23:16
【问题描述】:

我正在使用ipywidgets.Label 在笔记本中显示文本。 基本问题,如何更改Label 中的文本对齐方式?它似乎是左对齐的,我希望它是右对齐的。我真的在文档中寻找它,但我找不到它。

谢谢

【问题讨论】:

    标签: python jupyter-notebook ipywidgets


    【解决方案1】:

    RZin 的回答有点用,但它实际上并没有对齐文本——它只是移动了包含文本的框。

    可以这样对齐标签文本:

    Label("LABEL", layout=Layout(display="flex", justify_content="flex-start")

    使用"flex-start" 左对齐,"center" 居中对齐,"flex-end" 右对齐。有also "space-between" and "space-around"

    可以复制并粘贴到笔记本中的完整示例:

    from ipywidgets import Label, Layout, HBox
    from IPython.display import display
    
    x = Label("Align Left", layout=Layout(display="flex", justify_content="flex-start", width="30%", border="solid"))
    y = Label("Align Center", layout=Layout(display="flex", justify_content="center", width="30%", border="solid"))
    z = Label("Align Right", layout=Layout(display="flex", justify_content="flex-end", width="30%", border="solid"))
    display(HBox([x,y,z]))
    

    【讨论】:

    • 被低估的答案。 :D
    【解决方案2】:

    我有类似的问题,并用 VBox 小部件包装了我的标签。

    from ipywidgets import VBox, Label, Layout
    
    VBox([Label('I want this to be in the center.')]
    ,layout=Layout(width='100%', display='flex' ,
    align_items='center'))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-22
      • 2014-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多