PythonIDE Docs
中文
简体中文

深色模式

语义颜色、材质背景、强制色彩方案和符号配色。

appui 中,外观由 系统语义色字符串环境色方案材质背景 共同决定。优先使用语义色和材质,让页面自动适配浅色、深色和高对比度环境。


#预期效果

示例会展示语义色、材质背景、强制色彩方案和符号配色在浅色/深色下的表现。

#系统语义色(随浅色/深色变化)

foreground_colorbackground(color=...)fillstroke 等接受颜色的位置,可直接传入下列字符串(不区分大小写;部分名称支持 snake_case 别名,如 secondary_label)。

背景与分组

  • systemBackground(别名:system_backgroundbackground
  • secondarySystemBackgroundsecondary_system_backgroundsecondary_background
  • tertiarySystemBackgroundtertiary_system_backgroundtertiary_background
  • systemGroupedBackgroundsecondarySystemGroupedBackgroundtertiarySystemGroupedBackground

标签与分隔

  • labelsecondaryLabeltertiaryLabelquaternaryLabel
  • separatoropaqueSeparator
  • placeholderTextplaceholder_textplaceholder

填充层级

  • systemFillsystem_fillfill
  • secondarySystemFilltertiarySystemFillquaternarySystemFill

系统调色板

  • systemBluesystemRedsystemGreensystemOrangesystemPurple
  • systemPinksystemYellowsystemTealsystemCyansystemIndigo
  • systemGraysystemBrownsystemMint
python
import appui

state = appui.State()

def body():
    return (appui.VStack([
        appui.Text("主标签色").foreground_color("label"),
        appui.Text("次要说明").foreground_color("secondaryLabel"),
        appui.Divider(),
        appui.Text("强调链接").foreground_color("systemBlue"),
    ], spacing=12)
    .padding()
    .frame(max_width=400)
    .background(color="systemGroupedBackground", corner_radius=12))

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

#preferred_color_scheme:强制浅色或深色

对子树应用 .preferred_color_scheme(scheme)scheme'light''dark'

python
import appui

state = appui.State(force="dark")


def set_light():
    state.force = "light"


def body():
    return (appui.VStack([
        appui.Text("强制深色环境下的卡片").foreground_color("label"),
        appui.Button("切换为浅色预览", action=set_light),
    ], spacing=16)
    .padding()
    .background(color="secondarySystemBackground", corner_radius=16)
    .preferred_color_scheme(state.force))

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

#材质背景:background(material=...)

使用 模糊材质 时,不要同时传 colormaterial 常用取值如下:

material 参数值说明
ultra_thinultra_thin_materialultraThinMaterial
thinthin_materialthinMaterial
regularMaterialregularregular_materialregularMaterial
thickthick_materialthickMaterial
ultra_thickultra_thick_materialultraThickMaterial
python
import appui

state = appui.State()

def body():
    return (appui.VStack([
        appui.Text("玻璃质感背景").foreground_color("label"),
        appui.Text("副标题").font("caption").foreground_color("secondaryLabel"),
    ], spacing=8)
    .padding(20)
    .background(material="regularMaterial", corner_radius=14))

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

background 还可配合 gradientopacitycorner_radius 等参数;详见 View.backgroundappui.py 文档字符串。


#实践建议

  1. 优先语义色:卡片与列表用 secondarySystemGroupedBackgroundlabel 等,减少硬编码 #RRGGBB
  2. 对比度:在 preferred_color_scheme('dark') 下仍用 label / secondaryLabel 分层,而不是纯灰。
  3. 材质与圆角background(material='regular', corner_radius=12) 在圆角矩形内裁剪材质,视觉更统一。
  4. 调试:用 preferred_color_scheme 固定一种方案,截图对比浅色与深色下的 systemFill 层级是否清晰。
python
import appui

# 仅构建视图树,验证修饰符链可序列化。
def body():
    return appui.Text("Hi").padding().background(material="thin", corner_radius=8)

tree = body()
assert tree is not None

#与 SF Symbols 搭配

导航与媒体类符号在深浅背景下都清晰,例如 chevron.leftplay.fillgearshape.fill。配合 Image(system_name=...)symbol_rendering_mode('hierarchical') 可得到分层着色效果。

python
import appui

state = appui.State()

def body():
    return appui.HStack([
        appui.Image(system_name="sun.max.fill").foreground_color("systemYellow"),
        appui.Image(system_name="moon.fill").foreground_color("systemIndigo"),
    ], spacing=24).padding()

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

更多媒体与符号用法见 appui-guide-media.md