Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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


Comment on Re^2: Directory and file selection with PERL/Tk
Download Code
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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://960552]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (9)
As of 2014-09-21 14:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (172 votes), past polls