By now I have a convoluted mess of FocusIn, FocusOut, validate
command, etc. but I don't really get it to work. The most frustrating
part is that I am near to certain that it can be done with ease and
elegance - and that I am just too ignorant GUI wise. Well, time to
have a break.
If anybody can shed some light on this issue it would be most heartily
appreciated.
Best regards
Helmut Giese
Something like this, perhaps?
entry .e -width 1 -validatecommand {onechar %W %S} -validate key
pack .e
proc onechar {w s} {
# If more than one char is inserted, the last char wins
set s [string range $s end end]
if {[regexp {[a-zA-Z]} $s]} {
$w delete 0 end
$w insert 0 $s
after idle [list $w configure -validate key]
return 1
}
bell
return 0
}
Hi Bryan,
>Something like this, perhaps?
"perhaps" ??? It's just what the doctor ordered. You're a fan of
understatements, aren't you.
Right as I imagined: a solution of ease and elegance.
> entry .e -width 1 -validatecommand {onechar %W %S} -validate key
> pack .e
>
> proc onechar {w s} {
> # If more than one char is inserted, the last char wins
> set s [string range $s end end]
>
> if {[regexp {[a-zA-Z]} $s]} {
> $w delete 0 end
> $w insert 0 $s
> after idle [list $w configure -validate key]
> return 1
> }
> bell
> return 0
> }
Just one more question: What need does the 'after idle ...' fill? I've
seen it before - hunting around today for entry examples - but I don't
understand it: You've just validated the user input, so why schedule
another validation?
Bryan, you saved my day, well, evening. Thanks a million and best
regards
Helmut Giese
You aren't scheduling a validation, you are resetting the "do
validation" bit on the entry. If you read the man page on input
validation you'll see it mentions that "-validate" will be set to "none"
if inside the validatecommand you change the value of the widget.
Since we are deleting the old contents and inserting the character input
by the user, we have do reset the -validate option. The advice in the
man age is to use 'after idle'.
Hi Bryan,
now that you put it right in front of my nose I can see it, too. I had
read it, of course, but somehow its meaning slipped by.
Thanks and best regards
Helmut Giese