KScript 文档
ZH

桌面应用

简介

KScript.UI 让脚本在 macOS、Windows 或 Linux 上打开原生窗口。你在脚本中描述窗口的布局和状态,由 KScript 负责绘制。无需编写其他内容:不需要 Swift、C#、XAML 或 shell 脚本。

要开始使用,请导入该包并编写一个Window { }块(XML 中为<KScript.Window>):

<KScript.Window title="Counter" width="320" height="200">
  <let name="count" value="0" />

  <Text size="32">{$count}</Text>
  <Row>
    <Button variant="primary" on_click="$count += 1">Add one</Button>
    <Button on_click="$count = 0" disabled="$count == 0">Reset</Button>
  </Row>
</KScript.Window>
import KScript.UI

Window(title: 'Counter', width: 320, height: 200) {
  $count = 0

  Text(size: 32) "{$count}"
  Row {
    Button(variant: primary, on_click: $count += 1) "Add one"
    Button(on_click: $count = 0, disabled: $count == 0) "Reset"
  }
}

kscript counter.kscript会打开窗口,窗口关闭时脚本结束。窗口需要桌面环境:在服务器上,或在没有显示器的 SSH 会话中,脚本会因UI错误而停止。

samples/life-lab/desktop.kscript是一个完整示例:带可点击棋盘、计时器和控件的生命游戏,它与终端版和浏览器版共享相同的规则。