tcl - 限制Tk entry widget中的输入字符数

标签 tcl tk-toolkit gtkentry

在 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/

相关文章:

tcl - 使用时钟命令进行日期验证

sqlite - sqlite3 eval中的tcl var替换

python - 如何在按下按钮时查看菜单?

python - .pyw tk 程序立即关闭?

python - 使用 Tkinter 的 GUI 应用程序 - 拖放

tcl - 只读模式下的 tk 条目背景颜色更改

string - 如何在不添加空格的情况下连接 TCL 中的字符串

R - 根据在下拉列表 (ttkcombobox) 中选择的值更新文本输入框 (tkentry)

signals - PyGTK - GtkEntry 在光标处插入信号不起作用