在 TCL 中,如何限制 entry
小部件中的输入字符数?
我有一个 TCL 字段,用户可以在其中输入一些文本,我希望限制文本的长度(例如不超过 30 个字符)。
有什么选择吗?
最佳答案
入口验证回调是方式,%P
替换是关键。来自 the docs :
%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.
这意味着我们这样做:
pack [entry .e -width 50 -validate all -validatecommand {
expr {[string length %P] <= 30}
}]
好了,您将无法对长度超过 30 的小部件进行任何编辑。(最好还添加一个 -invalidcommand
规范,以便用户可以找出发生了什么。)
关于tcl - 限制Tk entry widget中的输入字符数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26296926/