Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Checkbutton -text

by shortyfw06 (Beadle)
on Mar 10, 2012 at 16:13 UTC ( #958872=perlquestion: print w/replies, xml ) Need Help??
shortyfw06 has asked for the wisdom of the Perl Monks concerning the following question:

I am having a problem with the following code. It runs but the -text=> \$label is not printing the proper labels. Where am I mistaken?

use Tk; my $mw = new MainWindow; my $out_req_frm = $mw -> Frame()->pack(); my $output_lab = $out_req_frm -> Label(-text=>"Select desired output f +rom the following.", -font=>"ansi 10 bold")->pack(); my %chk_; my %chk; my %out_req; my $n = 1; $out_req1 = '1. Carpet Plot Data'; $out_req2 = "2. Laminate Properties"; $out_req3 = "3. Laminate Stresses"; $out_req4 = "4. Laminate Strains"; $out_req5 = "5. Circumferential and Radial Stresses/Strains"; $out_req6 = "6. Displacements"; $out_req7 = "7. Strains Per Ply"; $out_req8 = "8. Stresses Per Ply"; $out_req9 = "9. Failure Criteria Per Ply"; $out_req10 = "10. Automatic Search for Failure"; do { my $row = $n + 1; my $label = $out_req{$n}; print $label; $chk_{$n} = $out_req_frm -> Checkbutton(-text=> \$label, -variable=> \$chk{$n}, -offvalue=>"0", -onvalue=>"1")->pack(); #$chk_{$n} -> grid(-row=>$row, -column=>1, -sticky=>'w'); $n = $n + 1; } until ($n==11); MainLoop;

Thank you!

Replies are listed 'Best First'.
Re: Checkbutton -text
by Eliya (Vicar) on Mar 10, 2012 at 16:34 UTC

    Use an array instead of multiple $out_reqN variables with the index in their name.

    Also, -text doesn't expect a reference, but a normal string.

    ... my @out_req = ( '1. Carpet Plot Data', "2. Laminate Properties", "3. Laminate Stresses", "4. Laminate Strains", "5. Circumferential and Radial Stresses/Strains", "6. Displacements", "7. Strains Per Ply", "8. Stresses Per Ply", "9. Failure Criteria Per Ply", "10. Automatic Search for Failure", ); for my $label (@out_req) { $chk_{$n} = $out_req_frm -> Checkbutton(-text=> $label, ... }

    %chk / %chk_ would also more naturally be arrays (not hashes), as the index $n is just a counter.

    See perldata and perldsc.

Re: Checkbutton -text
by kenslater (Initiate) on Mar 11, 2012 at 00:17 UTC

    You probably want to use the -textvariable option, not -text

      Why?  As the checkbutton labels are static text, -textvariable doesn't really seem to make sense here.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://958872]
Approved by Eliya
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2017-02-25 08:18 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (365 votes). Check out past polls.