Skip to content

TextAlign 继承自: Enum

The horizontal alignment of text within an input box.

示例 ​

python
import flet as ft


def showcase_card(text_align: ft.TextAlign) -> ft.Container:
    return ft.Container(
        width=280,
        padding=12,
        border=ft.Border.all(1, ft.Colors.RED),
        border_radius=10,
        bgcolor=ft.Colors.SURFACE_CONTAINER_LOW,
        content=ft.Column(
            spacing=8,
            controls=[
                ft.Text(text_align.name, weight=ft.FontWeight.BOLD),
                ft.Container(
                    width=240,
                    height=130,
                    padding=10,
                    border=ft.Border.all(1, ft.Colors.OUTLINE),
                    border_radius=8,
                    bgcolor=ft.Colors.SURFACE,
                    content=ft.Text(
                        text_align=text_align,
                        value="Flet helps you build cross-platform Python apps from one codebase.",  # noqa: E501
                    ),
                ),
            ],
        ),
    )


def main(page: ft.Page):
    page.horizontal_alignment = ft.CrossAxisAlignment.CENTER

    page.appbar = ft.AppBar(title="TextAlign Showcase")
    page.add(
        ft.SafeArea(
            content=ft.Column(
                controls=[
                    ft.Text(
                        "Compare horizontal text alignment modes in the same "
                        "text block."
                    ),
                    ft.Row(
                        wrap=True,
                        spacing=12,
                        expand=True,
                        scroll=ft.ScrollMode.AUTO,
                        alignment=ft.MainAxisAlignment.CENTER,
                        controls=[
                            showcase_card(text_align) for text_align in ft.TextAlign
                        ],
                    ),
                ],
            ),
        )
    )


if __name__ == "__main__":
    ft.run(main)

枚举值 ​

名称类型默认值说明
LEFT—'left'Align the text on the left edge of the container.
RIGHT—'right'Align the text on the right edge of the container.
CENTER—'center'Align the text in the center of the container.
JUSTIFY—'justify'Stretch lines of text that end with a soft line break to fill the width of the container.
START—'start'Align the text on the leading edge of the container.
END—'end'Align the text on the trailing edge of the container.

关注我,学习大模型、大数据、Python等热门技术。