tk_chooseDirectory
Guide: Dialogs
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:
- On Windows and macOS, and on Linux with Wayland, the dialog is the file dialog of the platform. On Wayland it comes from the file chooser portal of the desktop.
- The other backends, also iOS, show a dialog that ztk draws with its widgets: a list of the folder and a field for the name of a folder in it. With an empty name, the dialog gives the folder that it shows.
- The headless backend has a fake dialog of the platform for the tests. With no answer from the test, it gives an empty string at once, as a cancel.
- In the web build, an app that runs from a URL has no file dialogs: the command does not exist.
-mustexist exists for the scripts of Tk. A folder that the dialog of a platform gives exists.
Options
| Option | Type | Default | Description |
|---|---|---|---|
-initialdir | path | empty | The folder that the dialog shows at the start. |
-mustexist | boolean | 0 | Gives only a folder that exists. |
-parent | window | . | The window that the dialog belongs to. |
-title | string | none | The 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 /nonexistentA 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 NONEReturns
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
- On Linux, ztk shows the dialog of the desktop through the portal. Tk 8.6.16 on X11 shows a dialog of its own.
- The errors have the
-errorcodeNONE. Tk 8.6.16 gives codes such asTK LOOKUP OPTIONandTK LOOKUP WINDOW.
Facts
- Capability
- None.
- Tcl release
- Tcl 9.1 and 8.6.
- Since
- ztk 1.0.0-beta.1