Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Perl Tk Button Disabled

by PilotinControl (Pilgrim)
on Oct 24, 2022 at 19:53 UTC ( #11147647=perlquestion: print w/replies, xml ) Need Help??

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

Good Evening Monks!

My question is simple. I need to keep a button disabled even though another button is capable of enabling the disabled button. My code is below in its simplest form. In the bigger program a sensor keeps the button disabled even though other buttons in the program can enable the button when the sensor is NOT covered. Thanks in advance.

use Tk; use strict; my $Main = MainWindow->new(); my $Frame = $Main->Frame(); $Frame->pack(); my $Button1 = $Frame->Button(-text=>'One', -command=>\&one); $Button1->pack(); my $Button2 = $Frame->Button(-text=>'Two', -command=>\&two); $Button2->pack(); my $Button3 = $Frame->Button(-text=>'Three', -command=>\&three); $Button3->pack(); my $Button4 = $Frame->Button(-text=>'Four', -command=>\&four); $Button4->pack(); MainLoop(); sub one() { print "Hello\n"; } sub two() { $Button1->configure(-state => "disabled"); print "State Disabled: Only Button Three Can Enable Not Button Four.\n +" } sub three() { $Button1->configure(-state => "normal"); print "State Normal\n"; } sub four() { $Button1->configure(-state => "normal"); print "State Normal\n"; }

Replies are listed 'Best First'.
Re: Perl Tk Button Disabled
by haj (Priest) on Oct 24, 2022 at 20:29 UTC
    My question is simple.

    May I ask you to spend some effort to actually write down your question?

    In your code, sub three and sub four do exactly the same thing, but you seem to expect them to behave differently. If sub four does $Button1->configure(-state => "normal");, then button 1 will be enabled, period. Maybe you want to query the state of your sensor in sub four? Only, I don't see any sensor in your code.

Re: Perl Tk Button Disabled
by GrandFather (Saint) on Oct 24, 2022 at 20:40 UTC

    Perhaps you want to condition setting the state of the button depending on sensor state?

    sub four() { my $buttonState = sensorIsOn() ? "disabled" : "normal"; $Button1->configure(-state => $buttonState); $buttonState = ucfirst $buttonState; print "State $buttonState\n"; }

    Update: Fixed typo (thanks AnomalousMonk)

    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

      I think we are getting close. Below is the sensor code that sends data to the Perl script. The perl script changes the state and color of the button. However my other button code still changes the color of the button whether the sensor is tripped or not.

      if ($data =~ /(?<![\w-])ON(?![\w-])/i) { $btn1->configure(-background => "gray", -foreground => "black"); $btn1->configure(-state => "disabled"); print "ON\n"; } if ($data =~ /(?<![\w-])OFF(?![\w-])/i) { $btn1->configure(-background => "green", -foreground => "black"); $btn1->configure(-state => "normal"); print "OFF\n"; }

      And this code below still changes the color of $btn1 whether it is disabled or not. It's fine to change the color when it's enabled. It's NOT fine to change the color when it's disabled.

      sub chg_btn { $btn1->configure(-background => "green", -foreground => "black"); }

        So you should now have all almost the pieces you need to work out how to manage not changing button color in chg_btn when it ought not be changed. The missing part is passing the current sensor state into chg_btn in some fashion. Where you set up button call backs you can:

        my %context; ... $button->bind('<ButtonRelease-1>' => sub{chg_btn(\%context, @_});

        and something similar wherever you set up the sensor handling callback/sub. You can then

        sub sensorHandler { my ($context, ...) @_; $context->{sensorOn} = $data !~ /(?<![\w-])OFF(?![\w-])/i; ... }

        and in your button handler you can

        sub chg_btn { my ($context, ...) = @_; $btn1->configure(-background => "green", -foreground => "black") if !$context->{sensorOn}; }
        Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
Re: Perl Tk Button Disabled
by tybalt89 (Monsignor) on Oct 24, 2022 at 20:53 UTC
    sub two() { $Button1->configure(-state => "disabled"); $Button4->configure(-state => "disabled"); # like this? print "State Disabled: Only Button Three Can Enable Not Button Four.\n +" }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11147647]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (2)
As of 2023-06-03 02:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How often do you go to conferences?






    Results (6 votes). Check out past polls.

    Notices?