name: textual-scaffolder description: 使用小部件、屏幕和CSS样式生成Textual(Python)TUI应用程序结构。 allowed-tools: Read, Write, Edit, Bash, Glob, Grep
Textual 脚手架生成器
使用Python和现代异步模式生成Textual TUI(终端用户界面)应用程序。
能力
- 生成Textual项目结构
- 创建自定义小部件和屏幕
- 设置基于CSS的样式
- 实现响应式属性
- 创建组件组合
- 使用textual.testing设置测试
使用场景
在以下情况下调用此技能:
- 使用Python构建终端用户界面
- 创建具有CSS样式的交互式命令行界面
- 实现多屏幕TUI应用程序
- 设置Textual项目结构
输入参数
| 参数 | 类型 | 是否必需 | 描述 |
|---|---|---|---|
| projectName | 字符串 | 是 | 项目名称 |
| screens | 数组 | 否 | 屏幕定义 |
| widgets | 数组 | 否 | 自定义小部件定义 |
生成模式
主应用程序
from textual.app import App, ComposeResult
from textual.widgets import Header, Footer, Static, Button, Input
from textual.containers import Container, Horizontal, Vertical
from textual.screen import Screen
class MainScreen(Screen):
"""主应用程序屏幕。"""
CSS = """
MainScreen {
layout: grid;
grid-size: 2;
grid-gutter: 1;
}
#sidebar {
width: 30;
background: $surface;
border: solid $primary;
}
#content {
background: $surface;
border: solid $secondary;
}
"""
def compose(self) -> ComposeResult:
yield Header()
yield Container(
Static("侧边栏", id="sidebar"),
Static("内容", id="content"),
)
yield Footer()
class MyApp(App):
"""主TUI应用程序。"""
BINDINGS = [
("q", "quit", "退出"),
("d", "toggle_dark", "切换深色模式"),
]
CSS_PATH = "styles.tcss"
def on_mount(self) -> None:
self.push_screen(MainScreen())
def action_toggle_dark(self) -> None:
self.dark = not self.dark
if __name__ == "__main__":
app = MyApp()
app.run()
自定义小部件
from textual.widget import Widget
from textual.reactive import reactive
from textual.message import Message
class Counter(Widget):
"""一个带有递增/递减功能的计数器小部件。"""
value = reactive(0)
class Changed(Message):
"""计数器值已更改。"""
def __init__(self, value: int) -> None:
self.value = value
super().__init__()
def render(self) -> str:
return f"计数: {self.value}"
def increment(self) -> None:
self.value += 1
self.post_message(self.Changed(self.value))
def decrement(self) -> None:
self.value -= 1
self.post_message(self.Changed(self.value))
CSS样式 (styles.tcss)
Screen {
background: $surface;
}
Header {
dock: top;
background: $primary;
}
Footer {
dock: bottom;
background: $primary;
}
Button {
margin: 1;
}
Button:hover {
background: $primary-lighten-1;
}
Input {
margin: 1;
border: tall $secondary;
}
Input:focus {
border: tall $primary;
}
.error {
color: $error;
}
.success {
color: $success;
}
数据表格小部件
from textual.widgets import DataTable
from textual.app import ComposeResult
class DataScreen(Screen):
def compose(self) -> ComposeResult:
yield DataTable()
def on_mount(self) -> None:
table = self.query_one(DataTable)
table.add_columns("姓名", "邮箱", "角色")
table.add_rows([
("爱丽丝", "alice@example.com", "管理员"),
("鲍勃", "bob@example.com", "用户"),
("查理", "charlie@example.com", "用户"),
])
依赖项
[project]
dependencies = [
"textual>=0.40.0",
]
[project.optional-dependencies]
dev = [
"textual-dev>=1.0.0",
]
目标流程
- tui-application-framework
- interactive-form-implementation
- dashboard-monitoring-tui