Tk::chooseDirectory - pops up a dialog box for the user to select a directory.


$widget->chooseDirectory( ?option value ...? );


The method chooseDirectory is implemented as a perl wrapper on the core tk "command" tk_chooseDirectory, and $widget is passed as the argument to the hidden -parent option.

The chooseDirectory method pops up a dialog box for the user to select a directory. The following option-value pairs are possible as command line arguments:

-initialdir dirname

Specifies that the directories in directory should be displayed when the dialog pops up. If this parameter is not specified, then the directories in the current working directory are displayed. If the parameter specifies a relative path, the return value will convert the relative path to an absolute path. This option may not always work on the Macintosh. This is not a bug. Rather, the General Controls control panel on the Mac allows the end user to override the application default directory.

-parent $widget

Makes $widget the logical parent of the dialog. The dialog is displayed on top of its parent window.

-title titleString

Specifies a string to display as the title of the dialog box. If this option is not specified, then a default title will be displayed.

-mustexist boolean

Specifies whether the user may specify non-existant directories. If this parameter is true, then the user may only select directories that already exist. The default value is false.


Perl does not have a concept of encoded filesystems yet. This means that operations on filenames like opendir and open still use byte semantics. Tk however uses character semantics internally, which means that you can get filenames with the UTF-8 flag set in functions like chooseDirectory, getOpenFile and similar. It's the user's responsibility to determine the encoding of the underlying filesystem and convert the result into bytes, e.g.

    use Encode;
    my $dir = $mw->chooseDirectory;
    $dir = encode("windows-1252", $dir);
    opendir DIR, $dir or die $!;

See also "When Unicode Does Not Happen" in perlunicode and "Unicode in Filenames" in perltodo.


    my $dir = $mw->chooseDirectory(-initialdir => '~',
                                   -title => 'Choose a directory');
    if (!defined $dir) {
        warn 'No directory selected';
    } else {
        warn "Selected $dir";


Tk::getOpenFile, Tk::getOpenFile


directory selection dialog