编写最终修改文本文件的UI,我想知道是否可以确保用户的输入在预定范围内。
例如,我希望用户只输入10000到99999之间的值
label .labelSalary -text "Enter Salary"
entry .entrySalary -width 20 -relief sunken -textvariable salary
答案 0 :(得分:0)
您希望entry
widget使用-validate
,-validatecommand
和-invalidcommand
选项:演示
#!/usr/bin/env tclsh
package require Tk
proc validateSalary {} {
expr {$::salary eq "" || (10000 <= $::salary && $::salary <= 99999)}
}
proc invalidSalary {} {
tk_messageBox -type ok -icon warning \
-title "Invalid Salary" \
-message "Salary $::salary is not in the requested range"
}
label .labelSalary -text "Enter Salary"
entry .entrySalary -width 20 -relief sunken -textvariable salary \
-validate focusout \
-validatecommand validateSalary \
-invalidcommand invalidSalary
pack .labelSalary .entrySalary -side left