Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

One character only in entry

6 views
Skip to first unread message

Helmut Giese

unread,
Oct 24, 2005, 1:26:00 PM10/24/05
to
Hello out there,
for once back in GUI land simple requirements soon lead to big
problems - as I am used to by now :)
Here's my current brick wall: I need an entry where the user can only
put /one letter/
1) no punctuation or digits allowed
2) if she/he types a second valid char it has to overwrite the first,
else it is ignored.

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

Bryan Oakley

unread,
Oct 24, 2005, 3:15:23 PM10/24/05
to
Helmut Giese wrote:
> Hello out there,
> for once back in GUI land simple requirements soon lead to big
> problems - as I am used to by now :)
> Here's my current brick wall: I need an entry where the user can only
> put /one letter/
> 1) no punctuation or digits allowed
> 2) if she/he types a second valid char it has to overwrite the first,
> else it is ignored.

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
}

Helmut Giese

unread,
Oct 24, 2005, 2:59:39 PM10/24/05
to
On Mon, 24 Oct 2005 19:15:23 GMT, Bryan Oakley
<oak...@bardo.clearlight.com> wrote:

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

Bryan Oakley

unread,
Oct 24, 2005, 3:49:54 PM10/24/05
to

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'.

Helmut Giese

unread,
Oct 25, 2005, 7:29:11 AM10/25/05
to

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

0 new messages