in reply to Directory and file selection with PERL/Tk

I use ActiveState, currently on 5.10, but this also worked on 5.6. The ActiveState port and functionality of the getOpenFile() method is great! It would do everything you want and more.

short demo program follows. Window will look like any other Windows application "file open", there is icon shortcut for Desktop, recently used, etc. All the drives on system are shown. You can filter by extension.I just put a .txt and .log button to show you how. It remembers where you were last time, so opening this window goes conveniently to last directory that you were in. I haven't used it for selecting multiple things but I'm sure it can do that too.

#!/usr/bin/perl -w use strict; use Tk; my $mw = MainWindow->new; $mw->geometry("400x400+0+0"); my $menu_f = $mw->Frame()->pack(-side=>'top',-fill=>'x'); my $menu_file = $menu_f->Menubutton (-text=>'File',-tearoff=>'false') ->pack(-side=>'left'); $menu_file->command(-label=>'Open', -command=> \&get_file); #other menu buttons shown here as examples ... #$menu_file->command(-label=>'Save', # -command=> \&save_log); #$menu_file->command(-label=>'Close', # -command=> \&close_log); #$menu_file->command(-label=>'Exit',-command=>\&exit_msg); sub get_file { my @types = (["Log files", [qw/.txt .log/]], ["All files", '*'], ); my $filepath = $mw->getOpenFile(-filetypes => \@types) or return(); print "$filepath selected\n"; return($filepath); } MainLoop;

Replies are listed 'Best First'.
Re^2: Directory and file selection with PERL/Tk
by elef (Friar) on Mar 20, 2012 at 09:22 UTC
    I've been using getOpenFile and I like it a lot... now I need something like getOpenFile for folder selection.
    I could only find chooseDirectory, which is simple to use and works OK, but it doesn't look like the native folder picker in Windows, and it's not exactly intuitive for the user. It took me 10 minutes to figure out how to create a new folder at the desired location instead of picking an existing one. (You need to overtype the name of the lowest-level folder 'existing_folder' in the entry box; if you type existing_folder/newfolder in the box, it returns the path existing_folder/existing_folder/newfolder. You also have to add your own code for creating the new folder.)
    Anyone know of a better solution?
    Sample chooseDirectory code for reference:
    use strict; use warnings; require Tk; my $mw = Tk::MainWindow->new; my $dir = $mw->chooseDirectory(-initialdir => '~', -title => 'Choose a folder'); if (!defined $dir) { print "No directory selected"; } else { print "Selected $dir"; } Tk::MainLoop();

    I tried FileDialog as well, and got this bug: http://www.perlmonks.org/?node_id=646083
    It would seem that FileDialog is buggy, and the best option is chooseDirectory... I can live with that, but it really needs a "Create new folder" button to be perfect.

      Hi elef. I'm trying to do something similar. I have a program that opens a directory and selects one of the subdirectories. However, I can't get it to create a new subdirectory if necessary. I tried typing over the name of the lowest subdirectory as you suggested, but that didn't work.

      Did you ever find a solution? If not, would you please re-explain how to create a new subdirectory using the chooseDirectory widget? Thanks.

        No, I'm still using this ugly and clumsy widget... I don't think there's an easily available better option.
        As far as creating a new subdirectory, the chooseDirectory returns a path, you create the directory. Parts of my code look something like this:
        my $folder; # in real code this is triggered by a button press $folder = $mw->chooseDirectory( -initialdir => '~', -title => 'Choo +se a folder',); # triggered after user made its folder choice # almost certainly won't work if user enters a multi-level folder stru +cture if (!-d "$folder") { print "\nCreating $folder"; mkdir "$folder" or abort("Can't create folder $folder: $! at line +" . __LINE__); # abort sub is my own code, use die if needed }