beetlebug▾
/

tk_chooseDirectory

Shows a dialog to choose a folder, and gives its path.

tk_chooseDirectory ?-option VALUE ...?

Description

Shows a modal dialog of folders, waits until the user chooses a folder or cancels, and gives the path of the folder. A cancel gives an empty string.

A dialog gives paths, so it needs the file capability. In the jail of a root folder, the path is from the root.

The dialog depends on the backend:

-mustexist exists for the scripts of Tk. A folder that the dialog of a platform gives exists.

Options

OptionTypeDefaultDescription
-initialdirpathemptyThe folder that the dialog shows at the start.
-mustexistboolean0Gives only a folder that exists.
-parentwindow.The window that the dialog belongs to.
-titlestringnoneThe title of the window of the dialog.

-initialdir FOLDER

The folder that the dialog shows at the start.

In the dialog that ztk draws, an empty value or a folder that does not exist shows the current folder.

% tk_chooseDirectory -initialdir /nonexistent

A folder that does not exist is not an error.

-mustexist BOOLEAN

Gives only a folder that exists.

With a true value, the dialog that ztk draws does not accept the name of a folder that does not exist. With a false value, the user can type the name of a new folder, and the script makes it.

% tk_chooseDirectory -mustexist 1
% tk_chooseDirectory -mustexist maybe
expected boolean value but got "maybe"
-errorcode NONE

-parent WINDOW

The window that the dialog belongs to.

The dialog shows over the toplevel of WINDOW. The window must exist.

% tk_chooseDirectory -parent .
% tk_chooseDirectory -parent .nope
bad window path name ".nope"
-errorcode NONE

-title TEXT

The title of the window of the dialog.

The default is Choose Directory, in the language of msgcat for the dialog that ztk draws.

% tk_chooseDirectory -title "Choose a folder"
% tk_chooseDirectory -title
value for "-title" missing
-errorcode NONE

Returns

The path of the folder, or an empty string after a cancel.

Examples

# The headless backend answers with a cancel.
% tk_chooseDirectory -title "Choose the folder of the project"

Errors

% tk_chooseDirectory -name a.txt
bad option "-name": must be -initialdir, -mustexist, -parent, or -title
-errorcode NONE

% tk_chooseDirectory -title
value for "-title" missing
-errorcode NONE

Differences from Tcl

Facts

Capability
None.
Tcl release
Tcl 9.1 and 8.6.
Since
ztk 1.0.0-beta.1

See also