【问题标题】:Perl Tk window autoclosing on browsing directoryPerl Tk 窗口在浏览目录时自动关闭
【发布时间】:2017-09-13 16:32:29
【问题描述】:

我正在使用 Perl 的 Tk 模块开发桌面客户端。我有一个按钮可以打开特定任务的目录。但我面临的问题是它关闭了我不想关闭的 Perl 界面。

下面是实现目录打开逻辑的子:

sub open_directory {
  my $directory = shift;
  print "comes here atleast for $directory";
  if ($^O eq 'MSWin32') {
    exec "start $directory";
  }
  else {
    die "cannot open folder on your system: $^O";
  }
} 

我通过以下方式调用这个子:

sub second_window{

    my $row = 0;
    $mw2 = new MainWindow; 

    #Loop for listing taskname,path and browse button for all tasks of a region
    for my $i(1..$#tasks_region_wise){
        $row = $row+1;
        $frame_table-> Label(-text=>$sno+$i,-font=>"calibri")->grid(-row=>$row,-column=>0,-sticky=>'w');
        $frame_table-> Label(-text=>$tasks_region_wise[$i][2],-font=>"calibri")->grid(-row=>$row,-column=>1,-sticky=>'w');
        $frame_table-> Label(-text=>$tasks_region_wise[$i][3],-font=>"calibri")->grid(-row=>$row,-column=>2,-sticky=>'w');


#Calling that sub in the below line:

        $frame_table->Button(-text => 'Browse',-relief =>'raised',-font=>"calibri",-command => [\&open_directory, $tasks_region_wise[$i][3]],-activebackground=>'green',)->grid(-row=>$row,-column=>3);
        $frame_table->Button(-text => 'Execute',-relief =>'raised',-font=>"calibri",-command => [\&open_directory, $tasks_region_wise[$i][4]],-activebackground=>'green',)->grid(-row=>$row,-column=>4);
        $frame_table->Button(-text => 'Detail',-relief =>'raised',-font=>"calibri",-command => [\&popupBox, $tasks_region_wise[$i][2],$tasks_region_wise[$i][5]],-activebackground=>'green',)->grid(-row=>$row,-column=>5);

    }
    $frame_table->Label()->grid(-row=>++$row);
    $frame_table->Button(-text => 'Back',-relief =>'raised',-font=>"calibri",-command => \&back,-activebackground=>'green',)->grid(-row=>++$row,-columnspan=>4);

    MainLoop;
}

它会正确打开文件资源管理器窗口,但会关闭 Perl 界面。

【问题讨论】:

  • 我认为问题在于 exec 调用,它将您当前正在运行的可执行文件替换为新的。
  • 感谢 @ulix 的这一点,我已经通过使用系统函数而不是 exec 调用来解决它。

标签: perl tk


【解决方案1】:

张贴以供将来遇到此问题的任何人参考。正如 Stackoverflow 用户 @ulix 所评论的那样,我刚刚遇到了正确的问题。

问题:这里的问题是 exec 调用导致当前脚本执行停止并执行了启动目录命令。

解决方案:将 exec 调用转换为不会触发 exec 而是由 Perl 处理的系统调用。

PFB子的更新代码:

sub open_directory {
  my $directory = shift;
  print "comes here atleast for $directory";
  if ($^O eq 'MSWin32') {
    system("start $directory");
  }
  else {
    die "cannot open folder on your system: $^O";
  }
} 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-24
    • 2014-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-24
    相关资源
    最近更新 更多