Perl Tk窗口在浏览目录上自动关闭

时间:2017-09-13 08:15:01

标签: perl tk

我正在使用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;
}

它会正确打开File explorer窗口,但会关闭Perl界面。

1 个答案:

答案 0 :(得分:2)

发布以供任何面临此问题的人参考。我得到了一个正确的问题,由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";
  }
}