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!

regarding keybindings

Status
Not open for further replies.

RajVerma

Programmer
Jun 11, 2003
62
DE
hi all,

I have an entry widget and I want only the numbers to be entered in this entry but not any other character from the keyboard. so cud some one point out some useful links of some procedures.

thanx in advance.
Raj.
 
Use the "validate" and "validatecommand" options of the entry widget.
From the help file:
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.


so, you might do something like:

entry .abc -textvariable numsonly -validate all -validatecommand {regsub -all {^[0-9]} $numsonly {} numsonly}



Bob Rashkin
rrashkin@csc.com
 
hi bob,

thanx for the tip, but when using this {regsub -all {^[0-9]} $numsonly {} numsonly} command I'm not able to insert any character in my entry widget. I guess this expression blocked all the keys from the keyboard!!

Raj.
 
oops.

Try: regsub -all {[^0-9]} $numsonly {} numsonly

Slso, Maybe the "-validate all" is wrong, too. Try "-validate focusout".

Bob Rashkin
rrashkin@csc.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top