Home > Archive > Tcl > February 2007 > Re: Multiple Line Text Entry Widget - how to limit input or validate
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Re: Multiple Line Text Entry Widget - how to limit input or validate
|
|
| Bryan Oakley 2007-02-28, 7:10 pm |
| tab@tbsol.de wrote:
> Hy TCL/TK Programmers:
> I want to use a widget to get multipile lines of text from the user.
>
> There for I used "text".
>
> The question is, how does the code loke to limit the text entry to 255
> charakters?
> Is there a validation funktionallity?
> Is there another way like "entry" (which works only with one line).
Whenever data gets added to or removed from a widget, that widget's
command is called. You can place an execution trace on that command to
call a procedure of your choice when that happens. In that procedure you
can do any validation that you wish.
Here's a quick 'n' dirty example:
text .text
trace add execution .text leave validateData
pack .text -side top -fill both -expand 1
proc validateData {commandString code result op} {
set path [lindex $commandString 0]
set subcmd [lindex $commandString 1]
# N.B. "replace" is new for tk 8.5
if {$subcmd eq "insert" || $subcmd eq "replace"} {
# limit the data to 255 characters
set data [$path get 1.0 end-1c]
if {[string length $data] > 255} {
set insert [$path index insert]
set data [string range $data 0 254]
$path delete 1.0 end
$path insert 1.0 $data
$path mark set insert $insert
}
}
}
--
Bryan Oakley
http://www.tclscripting.com
| |
| Bryan Oakley 2007-02-28, 7:10 pm |
| tab@tbsol.de wrote:
> On 28 Feb., 17:28, Bryan Oakley <oak...@bardo.clearlight.com> wrote:
>
> TX so far. is this the only methode. looks ugly ..
> I will try this ..
>
No, it is not the only way. You have the option of messing with the
bindings, but personally I don't like that because there are potentially
many ways for the text to be modified (text insertion, pasting with
keyboard, pasting with mouse, ...); you need to be diligent to cover
them all and hope that a future maintenance programmer doesn't add some
new binding without adding the validation.
If you prefer to close your eyes to the details you can use the "wcb"
package (http://www.nemethi.privat.t-online.de/wcb/index.html) to hide
some of the complexity.
--
Bryan Oakley
http://www.tclscripting.com
| |
| Bruce Hartweg 2007-02-28, 7:10 pm |
| tab@tbsol.de wrote:
> On 28 Feb., 17:28, Bryan Oakley <oak...@bardo.clearlight.com> wrote:
>
> TX so far. is this the only methode. looks ugly ..
> I will try this ..
>
I don't really follow the looking ugly part, but if you don't like
command traces, you could also trigger off of the <<Modified>> virtual
event to do you checking. Or go poke around the wiki for pre-made
versions - looking at http://wiki.tcl.tk/text I see at least 3 examples
of multiline entry implementations.
Bruce
| |
| Bruce Hartweg 2007-02-28, 7:10 pm |
| Bryan Oakley wrote:
> tab@tbsol.de wrote:
>
> No, it is not the only way. You have the option of messing with the
> bindings, but personally I don't like that because there are potentially
> many ways for the text to be modified (text insertion, pasting with
> keyboard, pasting with mouse, ...); you need to be diligent to cover
> them all and hope that a future maintenance programmer doesn't add some
> new binding without adding the validation.
>
you can also make use of the <<Modified>> virtual event as well
> If you prefer to close your eyes to the details you can use the "wcb"
> package (http://www.nemethi.privat.t-online.de/wcb/index.html) to hide
> some of the complexity.
>
>
You can also look on the wiki for pre-done code looking at
http://wiki/tcl/tk/text
I see at least 3 examples of creating multiline entry widgets.
Bruce
|
|
|
|
|