Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Odd results using keyboard entry 1

Status
Not open for further replies.

satguy

Programmer
Jun 30, 2005
6
US
I've run into a situation that I can't explain and was wondering if anyone else has run across this. When I use an entry to allow user keyboard input, and restrict the key strokes to be numbers (0-9), I encounter a problem trying to enter some numbers after the initial number entered is a 0. Here are some cases: 1) If the first number entered is a 0 and is then followed by a 7, an 8 or 9 can't be entered next, though all other numbers can be entered. 2) If the first number is a 0, and then the next one entered is an 8 or 9, they aren't shown in the entry window, but all other numbers can be entered. 3) And, if I enter a series of 0's followed by an 8 or 9, the same result occurs - the 8 or 9 is not shown in the entry window. My test code is below. Any ideas? If I change the "string is int" to "string is digit" the problem goes away.

#!/bin/sh
exec wish "$0" "$@"
#----------------
frame .fMain -relief raised -borderwidth 4
frame .flat -relief groove -borderwidth 4

label .lnum -text "Enter a number:" -width 15
entry .enum -validate key -vcmd {expr {[string is int %P] \
&& [string length %P] < 14 }} -width 15 -bg white
label .lOK -text "\nHit OK when finished.\n"

pack .fMain
pack .flat .lOK -in .fMain
pack .lnum .enum -in .flat -side left

button .butOK -text "OK" -bg cyan -borderwidth 3 \
-command "desThisWindow"
pack .butOK -in .fMain

#----
# destroy the window
#----

proc desThisWindow { } {
destroy .
}


 
Strings with a leading zero are classified by TCL as an octal number. And then "08.." or "09.." are impossible values.
Your solution with "string is digit" is ok., but there's a comment in the string manuel page:
"string is digit --> Any unicode digit character. Note that this includes characters outside of the [0-9] range".

To make it sure to receive only entries of [0-9], you should use:
"entry .enum -validate key -vcmd {expr {[regexp {^[0-9]+$} %P] && [string length %P] < 14 }} -width 15 -bg white"

GL
 
esjo -

Thanks for clearing this up. I am still new to tcl and appreciate the help.

satguy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top