英文:
Capturing scalar variable in Perl Tk radiobutton form
问题
我试图在提交按钮按下后捕获标记的单选按钮的值,但似乎无法让它工作。我想要的是在提交按钮按下时关闭表单/窗口,并保存标记的值并发送到store_it
子例程,以便后续使用。
use strict;
use Tk;
my $mw = MainWindow->new;
# 默认值
my $determined = 'games';
# 从目录获取菜单选项
my $dir_lib = '/usr/local/';
my @options = `ls $dir_lib`;
chomp @options;
s{^\s+|\s+$}{}g foreach @options;
$mw->title("Confirm Determination");
$mw->label("选择类别。");
$mw->configure();
my $chosen;
my $confirmed;
# 基于文件类型创建单选按钮
foreach (@options) {
my $chosen = $dir_lib . $_;
$mw->Radiobutton(
-text => $_,
-value => $_,
-variable => \$determined,
-justify => 'left',
-padx => 30,
-pady => 10
)->pack( -anchor => 'w' );
}
$mw->Button(-text => "提交",
-command => \&store_it)->pack();
MainLoop;
###################
sub store_it {
##################
my $fts = $determined;
print "将要保存的值为:$fts\n";
####################
} # 存储它的结尾
####################
而不是表单保持活动状态,我会得到以下输出:
将要保存的值为:games
英文:
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)
答案1
得分: 2
尝试使用一个匿名子例程,调用store_it()
并传入$determined
的值,例如:
# ... 脚本的这部分与之前一样
my $confirmed;
# 根据文件类型创建单选按钮
foreach (@options) {
$mw->Radiobutton(
-text => $_,
-value => $_,
-variable => \$determined,
-justify => 'left',
-padx => 30,
-pady => 10
)->pack( -anchor => 'w' );
} # 结束循环
$mw->Button(-text => "提交",
-command => sub {store_it($determined)})->pack();
MainLoop;
###################
sub store_it {
##################
my ($fts) = @_;
print "将存储 $fts\n";
####################
} # 存储它的结束
####################
希望这对你有所帮助。如果你有任何其他问题,请随时提出。
英文:
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
####################
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论