KScript 文档
ZH

桌面应用

输入

bind: $name 会将 TextBox、CheckBox、Select 或 Slider 与一个状态变量绑定。当用户更改输入时,变量随之更改,窗口会重新绘制。在 on_change 中,$event.value 保存新值:

<KScript.Window title="Settings">
  <let name="name" value="''" />
  <let name="dark" value="false" />
  <let name="size" value="14" />
  <let name="theme" value="'teal'" />

  <Column gap="8">
    <TextBox bind="$name" placeholder="Your name" />
    <CheckBox bind="$dark">Dark mode</CheckBox>
    <Slider bind="$size" min="10" max="24" step="1" />
    <Select bind="$theme" options="['teal', 'orange', 'purple']" />
  </Column>
</KScript.Window>
import KScript.UI

Window(title: 'Settings') {
  $name = ''
  $dark = false
  $size = 14
  $theme = 'teal'

  Column(gap: 8) {
    TextBox(bind: $name, placeholder: 'Your name')
    CheckBox(bind: $dark) "Dark mode"
    Slider(bind: $size, min: 10, max: 24, step: 1)
    Select(bind: $theme, options: ['teal', 'orange', 'purple'])
  }
}

Select 的 options 可以是列表、由 { label, value } 映射组成的列表,或从标签到值的映射。

TextBox 接受与页面上 @bind 相同的绑定选项。on: 为 input(输入时即更新,默认值)、change,或 blur(焦点离开或按下 Enter 时更新)。trim 会去除首尾空格,as:(string、int、number 或 bool)会将值读取为该类型,并在值无效时显示错误:

<KScript.Window title="Age">
  <let name="age" value="0" />

  <TextBox bind="$age" on="blur" trim="yes" as="int" />
  <Text>Next year you'll be {$age + 1}</Text>
</KScript.Window>
import KScript.UI

Window(title: 'Age') {
  $age = 0

  TextBox(bind: $age, on: blur, trim, as: int)
  Text "Next year you'll be {$age + 1}"
}

multiline: true使TextBox容纳多行,on_enter在按下 Enter 时运行。