Advanced Search
Mail us
Home    Manuel    Links    Faq    Submit a tcl/tk Reference    Contact Us

    Some examples tcl/tk codes     Man of Bwidget     Man of blt     mkwidget screenshots     TkOgl     Video Demo real.


  How can I limit entry widget input (length or type of chars)?
 
 You want to make use of combination of the `entry` widget`s
`-textvariable` option and the tcl variable `trace` function.  Here is a
limited example:

proc forceInt {name el op} {
    global $name ${name}_int
    if {[string compare {} $el]} {
	set old  ${name}_int($el)
	set name $name($el)
    } else { set old ${name}_int }
    if {![regexp {^[-+]?[0-9]*$} [set $name]]} {
	set $name [set $old]
	bell; return
    }
    set $old [set $name]
}

pack [label .la -text {Integer 2: {^[-+]?[0-9]*$}}] -anchor w
pack [entry .a -textvariable myVar] -fill x -expand 1
trace variable myVar w forceInt
set myVar {}

You can see a more complex example as a Tk plugin
(http://www.scriptics.com/software/plugin.html) at
http://www.purl.org/net/hobbs/tcl/tclet/entrylimit.html.