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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

numbers only

Status
Not open for further replies.

RajVerma

Programmer
Jun 11, 2003
62
DE
hi all,

In the entry field of my GUI, I want to restrict the user to enter only numbers. So I want to make only the number keys from 0 to 9 to work and the rest to not to work in that particular entry widget. is it possible? or is there any other way to achieve this?

thanx,
Raj.
 
You should use the "-validate" option together with the "-validatecommand" option:
VALIDATION
Validation works by setting the validateCommand option to a script which will be evaluated according to the validate option as follows:

none
Default. This means no validation will occur.
focus
validateCommand will be called when the entry receives or loses focus.
focusin
validateCommand will be called when the entry receives focus.
focusout
validateCommand will be called when the entry loses focus.
key
validateCommand will be called when the entry is edited.
all
validateCommand will be called for all above conditions.
It is possible to perform percent substitutions on the validateCommand and invalidCommand, just as you would in a bind script. The following substitutions are recognized:

%d
Type of action: 1 for insert, 0 for delete, or -1 for focus, forced or textvariable validation.
%i
Index of char string to be inserted/deleted, if any, otherwise -1.
%P
The value of the entry should edition occur. If you are configuring the entry widget to have a new textvariable, this will be the value of that textvariable.
%s
The current value of entry before edition.
%S
The text string being inserted/deleted, if any, {} otherwise.
%v
The type of validation currently set.
%V
The type of validation that triggered the callback (key, focusin, focusout, forced).
%W
The name of the entry widget.
In general, the textVariable and validateCommand can be dangerous to mix. Any problems have been overcome so that using the validateCommand will not interfere with the traditional behavior of the entry widget. Using the textVariable for read-only purposes will never cause problems. The danger comes when you try set the textVariable to something that the validateCommand would not accept, which causes validate to become none (the invalidCommand will not be triggered). The same happens when an error occurs evaluating the validateCommand.

Primarily, an error will occur when the validateCommand or invalidCommand encounters an error in its script while evaluating or validateCommand does not return a valid tcl boolean value. The validate option will also set itself to none when you edit the entry widget from within either the validateCommand or the invalidCommand. Such editions will override the one that was being validated. If you wish to edit the entry widget (for example set it to {}) during validation and still have the validate option set, you should include the command

after idle {%W config -validate %v}

in the validateCommand or invalidCommand (whichever one you were editing the entry widget from). It is also recommended to not set an associated textVariable during validation, as that can cause the entry widget to become out of sync with the textVariable.
I've never done it so I'm not too sure about it, but I think the "key" value for the "-validate" option is what you want.

Bob Rashkin
rrashkin@csc.com
 
thanx Bob,

I tried this way before, but there is a drawback. It still allows non-number keys to be printed in the entry widget, but it truncates the string to only-numbers once the focus is out. but the problem here is it works only for the first focus out. For example if I enter "32f6gd" in the entry then it truncates it to "326" when I leave the entry widget, but only for the first time, so If I enter again some alpha-numeric characters then it does nothing.

This following code does the trick for me.

label .l1 -text "Integer:"
entry .e1 -validate key -vcmd {string is int %P}

I found this out at
thanx,
Raj.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top