Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^3: Dynamically add Optionmenus based on variable perl tk

by pashanoid (Scribe)
on Jul 29, 2011 at 08:15 UTC ( #917407=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Dynamically add Optionmenus based on variable perl tk
in thread Dynamically add Optionmenus based on variable perl tk

I've written up this example:

#!/usr/bin/perl use strict; use warnings; use Tk; my $mw = MainWindow->new; $mw->geometry("300x300"); my $var =1; my ($major, $other); my @options = ('one', 'two', 'three', 'four', 'five'); my $topics1=$mw->Radiobutton(-text =>'Selection 1', -value => '0', -variable => \$major, -command => sub {&do_smth('1')})->pack( +); my $topics2=$mw->Radiobutton(-text =>'Selection 2', -value => '1', -variable => \$major, -command => sub {&do_smth('2')})->pack() +; my $topics3=$mw->Radiobutton(-text =>'Selection 3', -value => '3', -variable => \$major, -command => sub {&do_smth('3')})->pack() +; if ($var eq 'more'){ my $options=$mw->Optionmenu( -options => [@options], -variable + => \$other, -command => [sub {&do_more($major, $other);}])->pack() +; } MainLoop; sub do_smth{ my $selection = shift; $var='more' unless ($selection == 2); } sub do_more{ my ($major, $other) = @_; print "do this: $other while Selection was $major\n"; }

How do I make the extra Optionmenu to appear when choice 2 is called?


Comment on Re^3: Dynamically add Optionmenus based on variable perl tk
Download Code
Replies are listed 'Best First'.
Re^4: Dynamically add Optionmenus based on variable perl tk
by Anonymous Monk on Dec 07, 2011 at 23:03 UTC
    In the latest release of Tk you can use $options->options() to reset the list whenever you like. Here is my version of your test case
    #!/usr/bin/perl use strict; use warnings; use Tk; my $mw = MainWindow->new; $mw->geometry("300x300"); my $var =1; my ($major, $other); my @options = ('one', 'two', 'three', 'four', 'five'); my $topics1=$mw->Radiobutton(-text =>'Selection 1', -value => '0', -variable => \$major, -command => sub {&do_smth('1')})->pack( +); my $topics2=$mw->Radiobutton(-text =>'Selection 2', -value => '1', -variable => \$major, -command => sub {&do_smth('2')})->pack() +; my $topics3=$mw->Radiobutton(-text =>'Selection 3', -value => '3', -variable => \$major, -command => sub {&do_smth('3')})->pack() +; my $options=$mw->Optionmenu( -options => [@options], -variable => \$ot +her, -command => [sub {&do_more($major, $other);}])->pack() +; MainLoop; sub do_smth{ my $selection = shift; if ($selection == 2) { $options->options ([@options, 'six' , 'seven']) } else { $options->options ([@options] ) } $var='more' unless ($selection == 2); } sub do_more{ my ($major, $other) = @_; print "do this: $other while Selection was $major\n"; }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://917407]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (15)
As of 2015-07-31 16:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (279 votes), past polls