PythonIDE Docs
中文
简体中文

形状 API

Rectangle、RoundedRectangle、Circle、Capsule、Ellipse、Color 和 ToolbarItem。

本页覆盖 RectangleRoundedRectangleCircleCapsuleEllipseColorToolbarItem。形状是普通 View,通常要配合 .frame(...) 才能得到稳定尺寸。

#什么时候用

目标首选 API说明
矩形背景或占位Rectangle基础块状形状。
卡片背景RoundedRectangle需要圆角但不想用 .background(...) 时。
圆形头像、圆点Circle宽高相同最稳定。
胶囊按钮背景Capsule两端半圆的条状背景。
椭圆Ellipse非等宽高的椭圆。
纯色块Color用作背景层或色块视图。
工具栏项ToolbarItem放到 .toolbar([...]) 里。

#形状公共方法

API签名所属类型
.fill.fill(color: ColorLike) -> Self_Shape
.stroke.stroke(color: ColorLike, line_width: float = 1, **kwargs: Any) -> Self_Shape
python
import appui


def body():
    card = appui.ZStack([
        appui.RoundedRectangle(corner_radius=14)
            .fill("secondarySystemBackground"),
        appui.RoundedRectangle(corner_radius=14)
            .stroke("separator", line_width=1),
        appui.Text("Shape Card").font("headline"),
    ]).frame(height=120)

    return appui.NavigationStack(
        appui.VStack([card], spacing=16)
        .padding()
        .navigation_title("Shapes")
    )


appui.run(body)

#形状签名

API签名分类
RectangleRectangle()shape
RoundedRectangleRoundedRectangle(corner_radius: float = 10, cornerRadius: Optional[float] = None)shape
CircleCircle()shape
CapsuleCapsule()shape
EllipseEllipse()shape
ColorColor(value: Optional[ColorLike] = None, red: Optional[float] = None, green: Optional[float] = None, blue: Optional[float] = None, opacity: float = 1.0)shape
ToolbarItemToolbarItem(placement: str = 'automatic', content: Optional[View] = None, role: Optional[str] = None)presentation

#颜色

fillstrokeforeground_colorbackgroundColor 都接受 AppUI 的颜色表达:

表达示例
系统语义色"systemBlue""label""secondaryLabel""systemBackground""separator"
十六进制"#3366CC"
RGB / RGBA 元组(0.2, 0.4, 0.8)(0.2, 0.4, 0.8, 0.7)
整数0x3366CC
python
import appui


def body():
    return appui.NavigationStack(
        appui.VStack([
            appui.Color(value="systemIndigo").frame(height=28),
            appui.Color(red=0.9, green=0.2, blue=0.2, opacity=0.5).frame(height=28),
            appui.Circle().fill("#3366CC").frame(width=56, height=56),
            appui.Capsule().stroke("systemGreen", line_width=3).frame(width=160, height=44),
            appui.Ellipse().fill("systemOrange").frame(width=120, height=72),
        ], spacing=12)
        .padding()
        .navigation_title("Color")
    )


appui.run(body)

#ToolbarItem

ToolbarItem 必须放在 .toolbar([...]) 中。placement 使用 AppUI 的 snake_case 名称:"automatic""navigation_bar_leading""navigation_bar_trailing""bottom_bar""principal""keyboard"

python
import appui


def close_page():
    appui.dismiss()


def body():
    return appui.NavigationStack(
        appui.Text("Toolbar content")
        .padding()
        .navigation_title("Toolbar")
        .toolbar([
            appui.ToolbarItem(
                placement="navigation_bar_trailing",
                content=appui.Button(
                    action=close_page,
                    content=appui.Label("Done", system_image="checkmark"),
                ),
                role="close",
            )
        ])
    )


appui.run(body, presentation="sheet")

#与 Path / Canvas 的边界

API不同点
Rectangle / Circle 等形状原生常见形状,适合背景、边框、圆点、胶囊。
Path自定义矢量路径,适合不规则形状、曲线和弧。
Canvas命令式绘图表面,适合混合多种图元、文字和渐变。
.background(...)更适合普通卡片背景;不需要额外 ZStack。
python
import appui


def body():
    commands = [
        {"move": [50, 0]},
        {"line": [100, 90]},
        {"line": [0, 90]},
        {"close": True},
    ]

    return appui.NavigationStack(
        appui.VStack([
            appui.RoundedRectangle(corner_radius=8)
                .fill("systemBlue")
                .frame(width=120, height=64),
            appui.Path(commands=commands, fill="systemOrange", stroke="label", line_width=2)
                .frame(width=120, height=100),
        ], spacing=16)
        .padding()
        .navigation_title("Shape vs Path")
    )


appui.run(body)

#常见错误

错误正确做法
形状不设 .frame(...),显示尺寸不可控。给形状明确宽高或高度。
圆形宽高不一致。Circle().frame(width: height:) 使用相同宽高;非圆用 Ellipse
卡片背景都用 ZStack + RoundedRectangle普通卡片直接用 .background(..., corner_radius=...) 更简单。
ToolbarItem 使用旧 placement 名称。使用 "navigation_bar_trailing" 等 snake_case 值。
Color(...) 包语义色再传给 .fill(...).fill("systemBlue") 直接传颜色表达即可。

#相关文档

文档用途
图表与画布 APICanvas、DrawingContext、Path。
修饰符 APIbackground、overlay、clip_shape、shadow、toolbar。
深色模式指南语义颜色和深浅色适配。