假设我们有输入框和一个按钮。当用户按下按钮时,应从输入框中取出路径并打开相应的文件夹。我怎么能用Perl / TK做到这一点?提前致谢
答案 0 :(得分:3)
您可以对打开文件浏览器的命令行进程进行系统调用。在Windows上,这显然是start
命令,在Linux上可以使用gnome-open
或nautilus
之类的命令。
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";
}
}
答案 1 :(得分:2)
您可能想尝试像Tk::DirTree widget这样的小部件。
#!/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);
}