I am trying to capture the value of the marked radio button after submit is pressed but can't seem to get it to work. What I would like is for the form/window to close when submit is pressed and for the value marked to be saved and sent to the store_it subroutine so that it can be used.
use strict;
use Tk;
my $mw = MainWindow->new;
# default
my $determined = 'games';
# get menu options from directories
my $dir_lib = '/usr/local/';
my @options = `ls $dir_lib`;
chomp @options;
s{^\s+|\s+$}{}g foreach @options;
$mw->title("Confirm Determination");
$mw->label("Pick category.");
$mw->configure();
my $chosen;
my $confirmed;
#create radio buttons based on file types
foreach (@options) {
my $chosen = $dir_lib . $_;
$mw->Radiobutton(
-text => $_,
-value => $_,
-variable => \$determined,
-justify => 'left',
-padx => 30,
-pady => 10
)->pack( -anchor => 'w' );
} # end of foreach
$mw->Button(-text => "Submit",
-command => \&store_it($chosen))->pack();
MainLoop;
###################
sub store_it {
##################
my ($fts) = @_;
print "Will store $fts\n";
####################
} # EOS store it
####################
Instead the form stays active and I get the following output:
Use of uninitialized value $fts in concatenation (.) or string at Tk_radio_menu.pl line 50.
Will store
Tk::Error: Undefined subroutine &main::1 called at /usr/lib/x86_64-linux-gnu/perl5/5.34/Tk.pm line 251.
Tk callback for .button
Tk::__ANON__ at /usr/lib/x86_64-linux-gnu/perl5/5.34/Tk.pm line 251
Tk::Button::butUp at /usr/lib/x86_64-linux-gnu/perl5/5.34/Tk/Button.pm line 175
<ButtonRelease-1>
(command bound to event)
Try to use an anonymous sub which calls store_it()
with the value of $determined
, e.g.:
# ... this part of the script is as before
my $confirmed;
#create radio buttons based on file types
foreach (@options) {
$mw->Radiobutton(
-text => $_,
-value => $_,
-variable => \$determined,
-justify => 'left',
-padx => 30,
-pady => 10
)->pack( -anchor => 'w' );
} # end of foreach
$mw->Button(-text => "Submit",
-command => sub {store_it($determined)})->pack();
MainLoop;
###################
sub store_it {
##################
my ($fts) = @_;
print "Will store $fts\n";
####################
} # EOS store it
####################