2011-06-08 164 views
1

假設我們有一個輸入框和一個按鈕。當用戶按下按鈕時,它應該從輸入框中取出路徑並打開相應的文件夾。我如何使用Perl/TK來做到這一點?在此先感謝Perl TK打開文件夾

回答

3

您可能會對打開文件瀏覽器的命令行進程執行系統調用。在Windows上,這顯然是start命令,在Linux上可以運行如gnome-opennautilus

sub open_directory { 
    my $directory = shift; 

    if ($^O eq 'MSWin32') { 
    exec "start $directory"; 
    } elsif ($^O = 'linux') { 
    exec "gnome-open $directory" or 
    exec "kde-open $directory"; 
    # test for more OS cases 
    } else { 
    die "cannot open folder on your system: $^O"; 
    } 
} 
+0

會嘗試讓你知道。順便說一句,tanx – Allwyn 2011-06-21 08:10:51

+0

調用這個子關閉了我的初始界面窗口。有沒有辦法阻止? – Mohit 2017-09-12 09:57:16

+0

我很久沒有用過tk了。我建議你問一個新問題。 – 2017-09-27 15:55:39

2

你可能想嘗試一個像這樣的小工具。

#!/usr/bin/perl 
use strict; 
use warnings; 

use Tk; 
use Tk::DirTree; 

my $mw = MainWindow->new; 
$mw->title("Type path of directory and click OK"); 
$mw->geometry('400x300+'.int(($mw->screenwidth-400)/2).'+'.int(($mw->screenheight-300)/2)); 

my $dir = $mw->Entry(-text  => '', 
         -width  => 20, 
         -font  => 'Courier 12 bold', 
         -background => 'Orange', 
)->pack(-ipadx => 35); 

$dir->focus(); 

$mw->Button(-text  => 'Ok', 
      -font  => 'Courier 12 bold', 
      -background => 'Orange', 
      -command => sub{ dirwindow($dir) }, 
)->pack( -side  => 'left', 
      -ipadx  => 40 
); 

$mw->Button(-text  => 'Exit', 
      -font  => 'Courier 12 bold', 
      -background => 'Orange', 
      -command => sub { exit } 
)->pack( -side  => 'right', 
      -ipadx  => 40 
); 

MainLoop; 

sub dirwindow { 
    my $d = shift; 
    my $dir_val = $d->get; 
    my $dl = $mw->DirTree(-directory => $dir_val)->pack(-fill => 'both', -expand => 1); 
} 
+1

'listdirfiles'是否帶參數?定義說不,用途說,它確實... – 2011-06-08 22:08:46

+0

感謝您的答覆。但是這個代碼打開了與輸入相對應的文件夾。可能是我的問題有點不清楚。我想打開相應給定路徑的窗口。假如用戶輸入c:\,我想打開c:\驅動器窗口。 – Allwyn 2011-06-09 02:13:26

+0

@Donal Fellows是的,我看到我通過了一個參數,並沒有使用它。 listdirfiles應該以類似於:my $ d = shift; my $ dir_val = $ d-> get; – d5e5 2011-06-11 19:56:43