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.


In reply to Re: Tk DirSelect Buttons by pryrt
in thread Tk DirSelect Buttons by merrymonk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.