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

Dear Monks,
I have a perl tk menubar that I want to be disabled until a certain variable
is switched from off to on.(in this case $consensus_created) I know how to grey out my menu option, but have no
idea how to re-enable it. I tried using an "if" command within the menu but (unsurprisingly) it wasn't happy.

See below for my attempt at doing this:

sub option_menuitems { # create the menu items for the Options menu. ############################################# [ [qw/command/, 'Create Consensus', qw/-accelerator Ctrl-c -comman +d/=> \&create_consensus], '', [qw/command/, 'View Text 1', qw/-accelerator Ctrl-i if ($consens +us_created==0){-state disabled} -command/=> \&view_text1], [qw/command/, 'View Text 2', qw/-accelerator Ctrl-r -command/=> +\&view_text2], ]; } # end of option_menuitems
I need these menus to activate in order. Ie. the user MUST create a consensus
before they can operate the View Text 1 command, and they must view text 1 before
they can view text_2.

Sounds simple, has me confused. Any ideas ?
Thanks,
basm101

Replies are listed 'Best First'.
Re: Perl Tk Menus:Disabling and Re-enabling
by PodMaster (Abbot) on Dec 12, 2002 at 11:43 UTC
    Yeah, that doesn't compile.
    You're missing a / near your if statement.
    Also, you cannot embed such an if statement in there.
    Try this on for size:
    sub option_menuitems { # create the menu items for the Options menu. ############################################# [ [qw/command/, 'Create Consensus', qw/-accelerator Ctrl-c -comman +d/=> \&create_consensus], '', [ command => 'View Text 1', -accelerator => 'Ctrl-i', ( $consensus_created == 0 ) ? qw(-state disabled) : (), -command => \&view_text1 ], [qw/command/, 'View Text 2', qw/-accelerator Ctrl-r -command/=> +\&view_text2], ]; } # end of option_menuitems


    MJD says you can't just make shit up and expect the computer to know what you mean, retardo!
    ** The Third rule of perl club is a statement of fact: pod is sexy.

Re: Perl Tk Menus:Disabling and Re-enabling
by Tanalis (Curate) on Dec 12, 2002 at 11:56 UTC
    Heys,

    You need to use the menu->entryconfigure method, like this:

    # $item is the item number in the menuitems array $menu -> menu -> entryconfigure($item, -state => "normal");

    If you initially set the menu option to disabled, when you come to setting $consensus_created later in the code, you can use entryconfigure then to enable or disable (with -state => "disabled") the relevant menu options.

    Hope that helps..
    -- Foxcub

    Update: Clarified things a little.

      Thanks guys,
      Foxcub's entryconfigure suggestion worked a treat :)

      basm101