Skip to content

样式Styles ​

本页属于 GenPy Ren'Py 学习路线。教程内容面向 Ren'Py 8.x,并在页面末尾连接到 GenPy 的对应工作流。

学习目标 ​

  • 了解“样式Styles”在 Ren'Py 项目中的用途。
  • 能在一个最小项目里验证本章相关语法或工作流。
  • 知道发布前需要检查的常见风险,并能在 GenPy 中继续维护。

核心语法 ​

  • style
  • is
  • xalign
  • background
  • text style
  • style prefix

可运行示例 ​

text
style my_text:
    color "#ffffff"
    size 28
    font "fonts/SourceHanSansSC-Regular.otf"
    outlines [(2, "#000000", 0, 0)]

教程正文 ​

样式(Style)决定 UI 元素(文本、按钮、窗口、滚动条…)的外观:颜色、字体、间距、背景、 悬停态等。Ren'Py 的屏幕控件几乎都从某个 style 取默认外观。

1. 定义与修改样式 ​

text
style my_text:
    color "#ffffff"
    size 28
    font "fonts/SourceHanSansSC-Regular.otf"
    outlines [(2, "#000000", 0, 0)]

修改已有样式(追加/覆盖属性):

text
style say_dialogue:
    color "#eee"
    line_spacing 6

2. 样式继承 ​

用 is 让一个样式继承另一个,再做局部调整:

text
style big_button is button:
    xpadding 40
    ypadding 20

style big_button_text is button_text:
    size 36

3. 状态前缀(idle / hover / selected / insensitive) ​

按钮等可交互控件有多种状态,可用前缀分别设置:

text
style choice_button:
    background "#333"
    hover_background "#555"          # 悬停态背景
    selected_background "#0a6"        # 选中态

style choice_button_text:
    idle_color "#aaa"                 # 普通态文字色
    hover_color "#fff"                # 悬停态文字色
    selected_color "#ff0"

状态前缀:idle_、hover_、selected_、insensitive_、selected_hover_ 等。

4. 常用样式属性速查 ​

文本类:

属性含义
color文字颜色
size字号
font字体
bold / italic / underline粗/斜/下划线
outlines描边 [(宽, 颜色, x偏移, y偏移)]
line_spacing行距
text_align / xalign对齐
kerning字距

盒子/按钮类:

属性含义
background背景(颜色/图片/Frame)
xpadding / ypadding内边距
xmargin / ymargin外边距
xminimum / yminimum最小尺寸
xalign / yalign对齐
spacing子元素间距(box/grid)
hover_sound / activate_sound交互音效

5. 在屏幕语言里套用样式 ​

text
screen example():
    text "标题" style "my_text"
    textbutton "确定" action Return() style "big_button"

    # 也可用 style_prefix 批量给一组控件套前缀
    vbox:
        style_prefix "menu"      # 用 menu_button / menu_button_text 等
        textbutton "开始"
        textbutton "退出"

style_prefix "menu" 会让其下控件自动使用 menu_button、menu_button_text 等样式族。

6. 动态/内联样式属性 ​

屏幕语言里可直接写样式属性(无需单独 style):

text
text "提示" color "#f00" size 30 xalign 0.5
textbutton "OK" action Return() background "#06c" xpadding 30

7. style 与 GUI 变量的关系 ​

  • gui.rpy 里的 gui.* 变量是“配置入口”,最终被 screens.rpy 中的 style 引用。
  • 改全局外观优先改 gui.*(见第 17 章);只想改某控件再写 style。

8. Python 中操作样式 ​

text
init python:
    style.my_text.color = "#0f0"          # 等价于 style 语句
    style.button.set_parent("default")     # 重设父样式

常见坑 ​

  • 文本控件的文字样式通常是“控件名 + _text”(如 button → button_text)。
  • 想改“悬停变色”要改 hover_color/hover_background,不是基础属性。
  • 全局风格改 gui.* 更省事;直接堆 style 易碎、难维护。
  • outlines 用元组列表,描边宽度为 0 时常用于纯阴影偏移。

官方参考 ​

在 GenPy 里怎么做 ​

  • 在 GenPy 的代码模式中维护相关 .rpy 文件,先保持语法清晰,再逐步加入本章功能。
  • 用故事图谱、变量、校验或演出编辑器把“能运行”提升到“可维护”。
  • 导出前结合 Ren'Py Lint 和 GenPy 校验结果,确认脚本、素材和发布路径一致。

推荐继续阅读:

Built for GenPy creators.