Please Help: pTk Menu Generation 
Author Message
 Please Help: pTk Menu Generation

Please Help:

I am trying to create menus without hard-coding each menu command. I
want each menu item to execute the same command, but with a different
argument.

What I WISH would work is something like this:

$index = 0;
while( defined( $listOfMenuItems->[$index][0] ) )
{
    $thisMenuItemLabel = $listOfMenuItems->[$index][0];
    $thisMenuItemParameter = $listOfMenuItems->[$index][1];
    $menubutton->command(
              -label => $thisMenuItemLabel,
              -command => sub{ &myCommand( $thisMenuItemParameter ); },
              );
    $index++;

Quote:
}

This doesn't work, because $thisMenuItemParameter is passed to the
'-command' subroutine as a reference to the variable, which means that
each menu item executes &myCommand with the most recent value of
$thisMenuItemParameter as its argument. What I WANT is a way to pass the
value of $thisMenuItemParameter to the subroutine when the menu item is
created.

Thanks in advance for any suggestions.

Jeff
--
-------------------------------------------------------------------
Jeff George

-------------------------------------------------------------------



Sat, 11 Dec 1999 03:00:00 GMT  
 Please Help: pTk Menu Generation



Quote:
>Please Help:

>I am trying to create menus without hard-coding each menu command. I
>want each menu item to execute the same command, but with a different
>argument.

>What I WISH would work is something like this:

>$index = 0;
>while( defined( $listOfMenuItems->[$index][0] ) )
>{
>    $thisMenuItemLabel = $listOfMenuItems->[$index][0];
>    $thisMenuItemParameter = $listOfMenuItems->[$index][1];
>    $menubutton->command(
>              -label => $thisMenuItemLabel,
>              -command => sub{ &myCommand( $thisMenuItemParameter ); },
>              );
>    $index++;
>}

>This doesn't work, because $thisMenuItemParameter is passed to the
>'-command' subroutine as a reference to the variable, which means that
>each menu item executes &myCommand with the most recent value of
>$thisMenuItemParameter as its argument. What I WANT is a way to pass the
>value of $thisMenuItemParameter to the subroutine when the menu item is
>created.

>Thanks in advance for any suggestions.

>Jeff
>--
>-------------------------------------------------------------------
>Jeff George

>-------------------------------------------------------------------

What I have done is to pass a reference to the scalar and then write the
subroutine to expect a scalar reference as an argument, to wit:

    .
    .
    -command => [ \&routine, \$variable ],
    .
    .

sub routine
{

    .
    .
    if($$var_rev == $some_value)
    {
    .
    .
--
Bob McClure, Jr.       | Bobcat Open Systems, Inc. | Cumby:  800-929-5513


Tolerance is the virtue of people who don't believe anything.-G.K. Chesterton



Fri, 17 Dec 1999 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Please Help: Error during PTk installation

2. ptk install on HPUX, help please !

3. How is this implemented in pTk - please help

4. Help with menus please:

5. Please Help: deleting menu items

6. Tabbed Menu - is it part of pTk distribution

7. ptk vs pTk vs PTK

8. Please report problems to ptk-owner@lists.stanford.edu

9. Help with automatic man page generation ....

10. Need help with range generation

11. Need help debugging an image generation script

12. X resource handling between ptk-b8 and ptk-b10

 

 
Powered by phpBB® Forum Software