Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re^2: Directory and file selection with PERL/Tk

by elef (Friar)
on Mar 20, 2012 at 09:22 UTC ( #960552=note: print w/replies, xml ) Need Help??

in reply to Re: Directory and file selection with PERL/Tk
in thread Directory and file selection with PERL/Tk

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:
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.

Replies are listed 'Best First'.
Re^3: Directory and file selection with PERL/Tk
by jeri_rl (Initiate) on Jun 11, 2013 at 19:15 UTC

    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 }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://960552]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2018-02-19 07:02 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (259 votes). Check out past polls.