Elderian has asked for the wisdom of the Perl Monks concerning the following question:

Heya, fellow monks!

I've got a little problem here, and i want to hear your opinions about it:

I've got a little Tk-Application, and i want to select a directory in it.

The script shall run on other computers in my company as well, so i do not want to install further modules.

BTW: The System is Windows XP an IndigoPerl 5.6

Unfortunately Tk::FileSelect doesn't return directories, and i can't find Tk::chooseDirectory on my machine, which was recommended by some monks in the chatterbox.

So after some scrolling through the docs, i found Tk::DirTree, which looked very promising, but i cannot get the currently selected directory out of this widget, at least not in the following way:

my $currentDir = "D:/p/"; my $dirTreeFrame = $mainwin->Frame; $dirTreeFrame->pack(); my $scrolledDirTree = $dirTreeFrame->Scrolled('DirTree', -scrollbars = +> "oeos")->pack(); $scrolledDirTree->configure(-directory => $currentDir, -width => 35, -height => 35, -command=> sub {doCreate($currentDir)} +);
I found the following documentation:
...
Switch: -command
Specifies the callback to be called when the user activates on a directory (usually by double-clicking on the name of the directory). The callback is called with one argument, the complete pathname of the directory.
...

So what argument do i have to submit, and how do i get it? It's not -directory, or else the code above should work...

Thanks for your help

Elderian

Update:
I used one single line out of zentara's code, and it worked! Hooray....
Unfortunately i have to offer more than one drive, which is not easy in windows.

So i found Win32::GUI::BrowseForFolder, which seems to be just thing i always searched for! ;)

Thank you all for your help!

Replies are listed 'Best First'.
Re: How do i select a directory in Tk?
by zentara (Cardinal) on Jul 07, 2004 at 14:25 UTC
    Here is some sample code for DirTree. It prints the dirname with a right click, but you could easily modify it.
    #!/usr/bin/perl use strict; use Tk; require Tk::DirTree; #### DEBUG #### $main::DEBUG = 1; # The initial directory my $initial_dir = '/'; # The main window... my $main_window = new MainWindow( -title => 'one test' ); # A scrolled directory tree my $tree = $main_window->Scrolled( 'DirTree', -width => 35, -height => 25, -scrollbars => 'osoe', -background => 'White', -selectmode => 'single', -selectbackground => 'DarkBlue', -selectforeground => 'White', -showhidden => 1, -directory => $initial_dir ); $tree->pack( -expand => 'yes', -fill => 'both', -padx => 2, -pady => 2, -side => 'left' ); # create the menu to be used for poping up my $menu = $main_window->Menu( -tearoff => 0, -menuitems => [ [ 'Button' => 'What?', -command => sub { print STDERR "Item is '", ( $tree->selectionGet() )[0] +, "'\n"; } ] ] ); # Create the binding for the right mouse button $tree->bind( '<Button-3>' => [ sub { my $widget = shift; my $y = shift; my $current_item = $widget->nearest($y); my $previous_item = ( $widget->selectionGet() )[0]; $main::DEBUG and print STDERR "old :", $previous_item, +"\n"; $main::DEBUG and print STDERR "current:", $current_item, +"\n"; # Clear selection and then set it to the new item $widget->selectionClear(); $widget->selectionSet($current_item); ### $$$$ Doesn't work: &destructor never called $menu->OnDestroy( [ \&destructor, $widget, $previous_item +] ); # Displaying the menu... # $menu->Popup( # -popover => 'cursor', # -popanchor => 'nw' # ); $menu->post($widget->pointerxy); destructor($widget, $previous_item); }, Ev('y') ] ); sub destructor { my $widget = shift; my $item = shift; $main::DEBUG and print STDERR "in destructor, item:", $item, "\n"; # Set selection to a particular item... $widget->selectionClear(); $widget->selectionSet($item); } # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - $tree->waitVisibility(); $tree->selectionSet($initial_dir); MainLoop(); #### program sends above ####################

    I'm not really a human, but I play one on earth. flash japh
Re: How do i select a directory in Tk?
by davidj (Priest) on Jul 07, 2004 at 13:36 UTC
    Take a look at Tk::DirSelect. You might already have it installed. (perldoc Tk::DirSelect).

    davidj
Re: How do i select a directory in Tk?
by eserte (Deacon) on Jul 07, 2004 at 22:03 UTC
    If you have Tk804 installed, then you can try the chooseDirectory method. This one uses a native directory selector on Windows, and Tk::FBox on Unix, just like getOpenFile or getSaveFile.
Re: How do i select a directory in Tk?
by Grygonos (Chaplain) on Jul 07, 2004 at 14:37 UTC
    I roll my own.

    It's an example from a program I did.. It's specifically for windows, as there are a great deal of windows caveats in the code. but it works like a charm. Not tryin to say this is the best solution by any means, there are devs far better than I, but this is just what has worked for me.

    P.S. The DirTree widget does look nice (visually) if you can get it working