Perl TK 打开文件夹

发布于 2024-11-14 09:49:36 字数 78 浏览 2 评论 0原文

假设我们有输入框和一个按钮。当用户按下按钮时,它应该从输入框中获取路径并打开相应的文件夹。我怎样才能使用 Perl/TK 做到这一点?提前致谢

Say we are having entry box and a button. When user press the button, it should take path from entry box and open the corresponding folder. How can I do it using Perl/TK? Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云柯 2024-11-21 09:49:36

您可以对打开文件浏览器的命令行进程进行系统调用。在 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";
  }
} 

You might make a system call to the command line process that opens the file browser. On windows this is apparently the start command, on Linux something like gnome-open or nautilus would work.

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";
  }
} 
小梨窩很甜 2024-11-21 09:49:36

您可能想尝试像 Tk::DirTree 小部件。

#!/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);
}

You may want to try a widget like the 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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文