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!

no. of chars restriction on entry widget? 1

Status
Not open for further replies.

RajVerma

Programmer
Jun 11, 2003
62
DE
hi all,
how to put a restriction for the number of characters to be entered on an entry widget? When I'm using the attribute -width 4 it's just alters the size but not the number!!
thanq.
 
There isnt an option to limit the # of characters entered. What you'll need to do is put a keystroke bind on that entry widget. The bind should call a function that checks the length of the contents of the entry widget, if the contents is too long then remove the last character. If the bind is in place, the user will never be able to exceed your set width.
 
It's really the same thing but the entry widget has a "-validate" option and a "-validatecommand" option. They allow you to specify a script (proc) to use to check the entry data.

Bob Rashkin
rrashkin@csc.com
 
Note that the -validate and -validatecommand options were added to entry widgets as of Tcl/Tk 8.3. So, if you're working with an earlier version, you'll need to accomplish the validation through bindings, as smugindividual suggested. But here's an example of how to use the built-in entry validation mechanism that Bong suggested:

Code:
proc maxChars {max val} {
  if {[string length $val] > $max} {
    return 0
  } else {
    return 1
  }
}

entry .e -width 4 -validate all     -validatecommand {maxChars 4 %P}
pack .e

- Ken Jones, President, ken@avia-training.com
Avia Training and Consulting, 866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top