Popcorn Dave has asked for the wisdom of the Perl Monks concerning the following question:

Fellow monks,

I am in need of some explaination of focus in an entry widget.

I have written a program which I am almost completely happy with ( which is rare for me :] ) and I want to post it to craft, but it's got one small, but what I consider nagging, bug. It's a regular expressions practice program for beginners. You type in a regex, sample string, then you can see what it does.

My question is about the focus of the entry widget however. I'm trying to turn off the checkbox options if the user enters tr/// for their regex.

I've got the following code that doesn't seem to want to work right and I can't see why not.

Entry widget:

my $reg=$top->Entry(width=>30, -font=>12, -textvariable=>\$regex, -validatecommand=>\&setting, -validate=>'all', )->pack();

Checkbutton widget

my $i=$frame1->Checkbutton( -bg=>COLOR, -text=>'Ignore Case (i)', -command=>\&global, -activebackground=>COLOR, -onvalue=>'i', -offvalue=>' ', -variable=>\$i_status, -font=>'code12', -justify=>'left', -state=>$state, )->pack( -side=>'left', );

Subroutine

sub setting{ if (substr($regex,0,2)eq 'tr') {$state='disabled'} else {$state='active'}; }

What I'm curious about is, is it possible to assign -state via variable? I can't see why it wouldn't be.

Replies are listed 'Best First'.
Re: Question regarding focus and Tk
by Chmrr (Vicar) on May 02, 2002 at 21:14 UTC

    you'll want to use :

    $i->configure(-state=>substr($regex,0,2) eq 'tr' ? 'disabled' : 'active');

    See the Tk::options page for details.

    perl -pe '"I lo*`+$^X$\"$]!$/"=~m%(.*)%s;$_=$1;y^`+*^e v^#$&V"+@( NO CARRIER'

Re: Question regarding focus and Tk
by bmcatt (Friar) on May 02, 2002 at 20:00 UTC
    Untested, but I've used similar in real programs:
    It looks like the problem is that you're basing your Checkbutton's state on $state instead of \$state.

    What you've got there is setting -state to the current value of $state. You probably want to try using:

    my $i = $frame1->Checkbutton(..., -state => \$state, ...);

    If that doesn't work, you'll need to get fancier and dynamically adjust the state of your Checkbutton with something like the following:

    sub setting { $i->configure(-state => (substr($regex, 0, 2) eq 'tr' ? 'disabled' : 'normal' )); }

    Note: The above isn't use strict; compliant. To do that, you can just hand a closure coderef that has the right context to your Entry widget, but doing so is left as an exercise to the reader. :-)