捕获Perl Tk单选按钮形式的标量变量

2nc8po8w  于 2023-03-03  发布在  Perl
关注(0)|答案(1)|浏览(160)

我试图在按下提交后捕获标记单选按钮的值,但似乎无法使其工作。我希望在按下提交时关闭窗体/窗口,并保存标记的值并将其发送到store_it子例程,以便可以使用它。

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
####################

相反,表单保持活动状态,并得到以下输出:

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)
ar7v8xwq

ar7v8xwq1#

尝试使用一个匿名的sub函数,它使用$determined的值调用store_it(),例如:

# ... 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
####################

相关问题