in reply to Tk DirSelect Buttons
The documentation at Tk::DirSelect is rather sparse, and does seem to be missing making this critical bit of information explicit (which is silly, because that's the whole point of the module). The module has no real tests, and no example scripts. The documentation does have one useful feature: it recommends trying Tk::chooseDirectory, "which uses native system dialogs where available" and comes with Tk starting in v804 (current version is 804.034). The chooseDirectory documentation has an example... I'd probably head down that route, if I were you.
However, the variable naming in the DirSelect SYNOPSIS implies that $ds->Show() returns the directory. The following test script shows that's the case (and compares using DirSelect vs chooseDirectory).
use warnings; use strict; use Tk; my $mw = MainWindow->new(); foreach my $fn ( \&use_chooseDirectory, \&use_DirSelect ) { my $dir = $fn->(); if (!defined $dir) { warn 'No directory selected'; } else { warn "Selected $dir"; } } sub use_chooseDirectory { my $dir = $mw->chooseDirectory(-initialdir => '~', -title => 'Choose a directory'); return $dir; } sub use_DirSelect { use Tk::DirSelect; my $ds = $mw->DirSelect(); my $dir = $ds->Show(); return $dir; }
edit: sorry, forgot to say, this is just showing how to use the resulting directory: it's just a string returned from the Show method. You don't need to associate a method with it. Notice in my code that I never do the MainLoop. If I did, I would have hooked the call to use_DirSelect() to some button or control in the MainWindow (like 'File > Change Directory'); I would have done the same for use_chooseDirectory() as well. AFAICT, there's no real need to change the button text (and that would tend to confuse people, I think.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Tk DirSelect Buttons
by Anonymous Monk on Apr 19, 2018 at 14:41 UTC | |
by zentara (Cardinal) on Apr 19, 2018 at 15:04 UTC | |
by merrymonk (Hermit) on Apr 19, 2018 at 15:20 UTC |