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

Messing around with the Tk module for Perl, and I came into a stumbling block. I've tried various techniques and searched over google, but I can't seem to be able to return a true value of the checkbutton/ratiobutton is selected or not... Any suggestions?

$scalar = $mw->Checkbutton(-text => "checkbutton...")->pack;

Given this (or a variation), if the checkbutton is checked, how would I return a true value, or a value pre-set by the checkbutton itself?
  • Comment on pTk: return values of radio/check buttons

Replies are listed 'Best First'.
Re: pTk: return values of radio/check buttons
by pg (Canon) on Mar 18, 2003 at 07:02 UTC
    Hope this demo can help you to understand:
    use Tk; use strict; my $val; my $mw = new MainWindow; $mw->Checkbutton(onvalue => "I am selected", offvalue => "I am deselected", variable => \$val) ->pack; $mw->Entry(textvariable => \$val)->pack; MainLoop;
    In a real situation, more likely, you would do this:
    $mw->Checkbutton(onvalue => 1, offvalue => 0, variable => \$val);
    To check whether the Checkbutton is selected, just check whether $val is true, do something like:
    if ($val) { ... } else { ... }