Tutorial Pages‎ > ‎

Entry Signals -what does what?

With so many events emitted by the gnocl::entry widget along with the assignment of a traced variable, the question of what event is best suited for what task rapidly comes to mind. First of all, lets examine the -variable option. The following script shows how it makes form creation a cinch.

# EntryEventOptions.tcl

# the next line restarts using tclsh \
exec tclsh "$0" "$@"
package require Gnocl

set form [list Name Surname Address1 Address2 Town]

array set details {
    Name        "John"
    Surname     "Smith"
    Address1    "The Manor"
    Address2    "1, High Street"
    Town        "Anytown"

set box [gnocl::box -orientation vertical]

foreach key $form {
    set ent($key) [gnocl::entry -variable details($key)]
    $box add $ent($key)

set but1 [gnocl::button \
    -text "Puts Details" \
    -onClicked {
        parray details } ]

$box add $but1

set lab1 [gnocl::label]

gnocl::window -child $box

Adding the following lines below to the script and running it again shows that even when a variable is assigned, its still possible to track changes to the widget contents using the -onChanged event.

set ent(empty) [gnocl::entry \
    -align 1 \
    -onChanged {
        $lab1 configure -text {%<<span foreground="red">NUMBERS - YES!</span>} }]
$box add $lab1 -fill {1 1} -expand 1
$box add $ent(empty)

Entry Validation

Sometimes its necessary to restrict the range of values that can be entered, a phone number for instance has to be numeric. Extending the widget bindings to respond to keyPress events can be used to make such tests. Here, alphabetical values are not allowed. Only the entry of numbers will cause the changed signal to be emitted.

$ent(empty) configure -onKeyPress {
    if {[string is alpha %a] } {
        $lab1 configure -text {%<<span foreground="blue">LETTERS - NO!</span>}
        gnocl::signalStop %w %e    } }


The -onActivate option allows the programmer to decide what to do with the widget contents after the Enter key has been pressed.

$ent(empty) configure -onActivate {
    puts "Widget %w contains %t"

Finally, a quick glance at the documentation shows that there are other signal handlers too, such as -onDelete, -onInsert, -onCursor and so on. Ordinarily these options would not be used in most scripts, they are made available, however, as such bindings are essential in the implementation undo/redo functionality for this widget.

William Giddings,
26 Apr 2010, 11:47