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

Monks, I once again stand before you asking to be blessed with the great wisdom you posses!

I am creating A menubutton which when clicked produces the checkbutton menu. Now everything works like a charm, with the exception of the end desired result. See code below:

$b4 = $windows{overlord_left1}->Menubutton( -text => 'EC', -takefocus => 0, -background=>'black', -foreground=>'white', -relief => 'flat'); for $label(@ECs){ my $i=0; $i++; my $variable_name = "$label"."$i"; $label_ref = \$variable_name; chomp $label; $b4->checkbutton(-label => "$label", -variable => $label_ref, -command => sub { if ($variable_name > 0){ our $ec = "$label";}}); if($ec){ foreach (@systemSelected){ push @systemNames, "$_"."-$ec";} }

I am having to use a variable reference, as I have to somehow dynamically create the variable used for the checkbutton variable. Now this works except when I check more than 1 box. If only one checkbox is selected then the name is appended as expected, but if I select multiple checkboxes, I only get the output of ONE button :s Assuming this is a result of my dynamic variable, any suggestions otherwise?

Replies are listed 'Best First'.
Re: Perl/Tk - Dynamic Checkbuttons?
by zentara (Cardinal) on Mar 29, 2012 at 15:20 UTC
    Please post a complete working example that we can run. Make up arrays for the demo like q(a..z). From first glance, it appears you are trying to build variable names, when you should be using hash or an array. See how bad it is to use a variable as a variable name

    From a google search:

    #!/usr/bin/perl use warnings; use strict; my $part_2 = "Not a good idea"; my $var1 = "part"; my $var2 = "2"; my $compound = eval "\$${var1}_$var2"; print "$compound\n"; __END__ There are three important things to note about this: 1. it is a really bad idea 2. the first $ must be escaped to avoid the first evaluation (the interpolation into the string) 3. $var1 must be in the form of ${var1} because $var1_ is a valid vari +able name
    Post a complete minimal example, and we will show you how to use a hash instead. You can make a reference like \$hash{$name} Or if you prefer, arrays can be used like in the following.
    #!/usr/bin/perl use warnings; use strict; use Tk; use Tk::Pane; use Encode; my $topdir= shift || '.'; my @subdirs = get_sub_dirs($topdir); my $mw = tkinit(); $mw->geometry('400x400+100+100'); my $sp = $mw->Scrolled('Pane', -scrollbars=>'osoe', sticky=>'nwse') ->pack(-expand=>1, -fill=>'both' ); my @cbvalues; my @cbnames; my $count = 0; foreach my $d( @subdirs){ $cbnames[$count] = $d; $sp->Checkbutton(-text => $d, -font=>[arial => 12], -onvalue => 1, -offvalue => 0, -variable => \$cbvalues[$count], -font => 'big', -bg => 'white', )->pack(-anchor=>'w')->pack(); $count++; } my $showbutton= $mw->Button(-text=>'Show Selected', -bg => 'lightyellow', -command => sub{ my @selected = (); foreach my $c( 0.. $count ){ if ( $cbvalues[$c] ){ push @selected, $cbnames[$c]; } } print "@selected\n"; } )->pack(); MainLoop(); sub get_sub_dirs { my $dir = shift; opendir my $dh, $dir or die "Error: $!"; my @files = grep !/^\.\.?$/, readdir $dh; @files = map { decode( 'utf8', "$dir/".$_ ) } sort @files; closedir $dh; my @sdirs; for my $file ( @files ) { if(-d $file){push @sdirs,$file;} } return @sdirs; }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: Perl/Tk - Dynamic Checkbuttons?
by ~~David~~ (Hermit) on Mar 29, 2012 at 14:05 UTC
    Its kind of hard to infer what you are asking exactly, but could the problem be that you are re-initializing $i every time you loop through the @ECs? $i is always "1".