python - 在 Ipywidget 中更改 Label 内容的对齐方式

标签 python jupyter-notebook ipywidgets

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

谢谢

最佳答案

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

jupyter notebook example output

关于python - 在 Ipywidget 中更改 Label 内容的对齐方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58416763/

相关文章:

python - 为什么当我将鼠标悬停在 imshow 上时不显示像素值?

python - ipywidgets 小部件值没有改变

widget - 如何使用 IPython Widgets 让一个参数的可能值依赖于另一个参数?

python - 套接字错误 [错误 10060]

ruby - 在 ruby​​ 中使用 matplotlib.image 是否有等效项

java - 如何从 Java JAR 文件调用 Python 脚本

python - Requests.get 显示与 Chrome 开发者工具不同的 HTML

ipython - 从函数返回值,其中 ipython 小部件用于获取输入参数

python - 使用重复索引递增 Numpy 数组

python - PyQt Python - 为 QPushButton 创建鼠标右键单击