Tcl 中的 %P 是什么?

What is %P in Tcl?

我在 Tcl 中看到了这段代码:

entry .amount -validate key -validatecommand {
    expr {[string is int %P] || [string length %P]==0}
}

我知道这是一个输入验证,但是,代码中的“%P”是什么意思?我正在查看 Tcl 的文档,但什么也没找到。

我认为这是另一种方法,但它具有相同的符号:

proc check_the_input_only_allows_digits_only {P} {
    expr {[string is int P] || [string length P] == 0}
}
entry .amount \
    -validate key \
    -validatecommand {check_the_input_only_allows_digits_only %P}

entry 的 tcl-tk 页面说

%P

The value of the entry if the edit is allowed. If you are configuring the entry widget to have a new textvariable, this will be the value of that textvariable.

https://www.tcl.tk/man/tcl8.4/TkCmd/entry.html#M25

I think this is another way to do it but it has the same symbols:

你很接近。您只需要在几个地方使用 $,因为您只是 运行 一个过程,这对于在过程中使用参数是正常的。

proc check_the_input_only_allows_digits_only {P} {
    expr {[string is int $P] || [string length $P] == 0}
}
entry .amount \
    -validate key \
    -validatecommand {check_the_input_only_allows_digits_only %P}

建议您使用过程编写类似的东西,而不是最琐碎的验证(或其他回调);将复杂性直接放在回调中很快就会让人感到困惑。

我建议在输入阶段保持宽松的验证,并且只在表单提交时严格验证内容(或按 OK/Apply 按钮,或任何在GUI) 正是因为在输入时 确实 方便在许多表单中存在一段时间的无效状态。因此,每键验证可能应该只用于表明是否相信表单提交会起作用,而不是彻底阻止现有的瞬变。


string is int 命令 returns 对于零长度输入是正确的,因为它最初是为了与该验证机制一起使用。 实际 整数验证需要 string is int -strict,这让我很吃力。但是现在不能改变它;这只是一个错误的默认值…

entry .amount -validate key -validatecommand {string is int %P}