使用 BWidget 的 ComboBox 验证输入
BWidget ComboBox 小部件允许您在输入字段中填写值。我想仅强制该字段中的特定字符(例如仅[a-z0-9])。为此,我想使用 Tcl/Tk 的 -validatecommand (或简称 -vcmd),就像使用标准“entry”小部件一样:
proc ValidateMyEntry { value } {
# Check if it's alphanum string
if ![regexp {^[-a-zA-Z0-9]*$} $value] {
return 0
}
return 1
}
entry .my_entry -width 20 -textvariable myVar -validate key -vcmd {ValidateMyEntry %P}
似乎 ComboBox 不支持 -validatecommand。最好的解决办法是什么?
The BWidget ComboBox widget allows you to fill in an entry field with a value. I would like to enforce only specific characters in that field (e.g. only [a-z0-9]). For that purpose I would like to use Tcl/Tk's -validatecommand (or -vcmd for short), just as you do with the standard 'entry' widget:
proc ValidateMyEntry { value } {
# Check if it's alphanum string
if ![regexp {^[-a-zA-Z0-9]*$} $value] {
return 0
}
return 1
}
entry .my_entry -width 20 -textvariable myVar -validate key -vcmd {ValidateMyEntry %P}
It seems ComboBox does not support -validatecommand. What's the best work-around?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想使用 BWidget,可以尝试使用 -modifycmd 或 -postcommand。
无论如何,我建议您尝试使用 -postcommand 选项的 ttk::combobox。
If you want to use a BWidget, you can try with -modifycmd or -postcommand.
Anyway I would suggest you to try the ttk::combobox with the -postcommand option.
作为可能但有点麻烦的事情,我决定使用旧式的“跟踪变量”函数来强制组合框中的值。
将以下语句放在 ComboBox 调用之后:
在其他地方,您必须定义 forceAlphaNum 过程:
As something that was possible but a bit cumbersome, I decided to use the old-style 'trace variable' function to enforce values in combobox.
Put the following statement after the ComboBox call:
Elsewhere, you have to define the forceAlphaNum proc: