在Perl Tk单选按钮表单中捕获标量变量。

huangapple go评论85阅读模式
英文:

Capturing scalar variable in Perl Tk radiobutton form

问题

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

  1. use strict;
  2. use Tk;
  3. my $mw = MainWindow->new;
  4. # 默认值
  5. my $determined = 'games';
  6. # 从目录获取菜单选项
  7. my $dir_lib = '/usr/local/';
  8. my @options = `ls $dir_lib`;
  9. chomp @options;
  10. s{^\s+|\s+$}{}g foreach @options;
  11. $mw->title("Confirm Determination");
  12. $mw->label("选择类别。");
  13. $mw->configure();
  14. my $chosen;
  15. my $confirmed;
  16. # 基于文件类型创建单选按钮
  17. foreach (@options) {
  18. my $chosen = $dir_lib . $_;
  19. $mw->Radiobutton(
  20. -text => $_,
  21. -value => $_,
  22. -variable => \$determined,
  23. -justify => 'left',
  24. -padx => 30,
  25. -pady => 10
  26. )->pack( -anchor => 'w' );
  27. }
  28. $mw->Button(-text => "提交",
  29. -command => \&store_it)->pack();
  30. MainLoop;
  31. ###################
  32. sub store_it {
  33. ##################
  34. my $fts = $determined;
  35. print "将要保存的值为:$fts\n";
  36. ####################
  37. } # 存储它的结尾
  38. ####################

而不是表单保持活动状态,我会得到以下输出:

  1. 将要保存的值为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.

  1. use strict;
  2. use Tk;
  3. my $mw = MainWindow->new;
  4. # default
  5. my $determined = 'games';
  6. # get menu options from directories
  7. my $dir_lib = '/usr/local/';
  8. my @options = `ls $dir_lib`;
  9. chomp @options;
  10. s{^\s+|\s+$}{}g foreach @options;
  11. $mw->title("Confirm Determination");
  12. $mw->label("Pick category.");
  13. $mw->configure();
  14. my $chosen;
  15. my $confirmed;
  16. #create radio buttons based on file types
  17. foreach (@options) {
  18. my $chosen = $dir_lib . $_;
  19. $mw->Radiobutton(
  20. -text => $_,
  21. -value => $_,
  22. -variable => $determined,
  23. -justify => 'left',
  24. -padx => 30,
  25. -pady => 10
  26. )->pack( -anchor => 'w' );
  27. } # end of foreach
  28. $mw->Button(-text => "Submit",
  29. -command => \&store_it($chosen))->pack();
  30. MainLoop;
  31. ###################
  32. sub store_it {
  33. ##################
  34. my ($fts) = @_;
  35. print "Will store $fts\n";
  36. ####################
  37. } # EOS store it
  38. ####################

Instead the form stays active and I get the following output:

  1. Use of uninitialized value $fts in concatenation (.) or string at Tk_radio_menu.pl line 50.
  2. Will store
  3. Tk::Error: Undefined subroutine &main::1 called at /usr/lib/x86_64-linux-gnu/perl5/5.34/Tk.pm line 251.
  4. Tk callback for .button
  5. Tk::__ANON__ at /usr/lib/x86_64-linux-gnu/perl5/5.34/Tk.pm line 251
  6. Tk::Button::butUp at /usr/lib/x86_64-linux-gnu/perl5/5.34/Tk/Button.pm line 175
  7. <ButtonRelease-1>
  8. (command bound to event)

答案1

得分: 2

尝试使用一个匿名子例程,调用store_it()并传入$determined的值,例如:

  1. # ... 脚本的这部分与之前一样
  2. my $confirmed;
  3. # 根据文件类型创建单选按钮
  4. foreach (@options) {
  5. $mw->Radiobutton(
  6. -text => $_,
  7. -value => $_,
  8. -variable => \$determined,
  9. -justify => 'left',
  10. -padx => 30,
  11. -pady => 10
  12. )->pack( -anchor => 'w' );
  13. } # 结束循环
  14. $mw->Button(-text => "提交",
  15. -command => sub {store_it($determined)})->pack();
  16. MainLoop;
  17. ###################
  18. sub store_it {
  19. ##################
  20. my ($fts) = @_;
  21. print "将存储 $fts\n";
  22. ####################
  23. } # 存储它的结束
  24. ####################

希望这对你有所帮助。如果你有任何其他问题,请随时提出。

英文:

Try to use an anonymous sub which calls store_it() with the value of $determined, e.g.:

  1. # ... this part of the script is as before
  2. my $confirmed;
  3. #create radio buttons based on file types
  4. foreach (@options) {
  5. $mw->Radiobutton(
  6. -text => $_,
  7. -value => $_,
  8. -variable => $determined,
  9. -justify => 'left',
  10. -padx => 30,
  11. -pady => 10
  12. )->pack( -anchor => 'w' );
  13. } # end of foreach
  14. $mw->Button(-text => "Submit",
  15. -command => sub {store_it($determined)})->pack();
  16. MainLoop;
  17. ###################
  18. sub store_it {
  19. ##################
  20. my ($fts) = @_;
  21. print "Will store $fts\n";
  22. ####################
  23. } # EOS store it
  24. ####################

huangapple
  • 本文由 发表于 2023年2月26日 23:27:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75573003.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定