tk_getOpenFile
Guide: Dialogs
Shows a dialog to choose a file to open, and gives its path.
tk_getOpenFile ?-option VALUE ...?
Description
Shows a modal dialog of files and folders, waits until the user chooses a file or cancels, and gives the path of the file. A cancel gives an empty string. The dialog gives only the path: the script opens the file.
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.
- On iOS, the dialog is the document picker of the system.
- The other backends show a dialog that ztk draws with its widgets: a list of the folder, a field for the name, and a combobox of the file types.
- 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.
-message exists only with a dialog of the platform, and the dialogs of Windows show no message. The message of a bad option lists the options of the dialog of the backend.
Options
| Option | Type | Default | Description |
|---|---|---|---|
-defaultextension | string | empty | The extension of a name with none, for the file dialogs of Tk that save. |
-filetypes | list | empty | The filters of the dialog: a list of file types, each a name and its patterns. |
-initialdir | path | empty | The folder that the dialog shows at the start. |
-initialfile | string | empty | The name of the file that the dialog selects at the start. |
-message | string | empty | A text that the dialog of the platform shows over the files. |
-multiple | boolean | 0 | Lets the user choose more than one file. |
-parent | window | . | The window that the dialog belongs to. |
-title | string | none | The title of the window of the dialog. |
-typevariable | variable name | empty | A global variable with the name of the file type of the dialog. |
-defaultextension EXTENSION
The extension of a name with none, for the file dialogs of Tk that save.
An open dialog accepts the option for the scripts of Tk. The dialog of a platform can use it to select a filter.
% tk_getOpenFile -defaultextension .txtAny text is an extension.
-filetypes LIST
The filters of the dialog: a list of file types, each a name and its patterns.
Each element is a list of a name and a list of extensions or patterns, such as {Images {.png .jpg}}, with an optional third element of Mac file types. An extension .txt matches *.txt, and * matches each file. The dialog shows the files of the first type at the start, and the user can select another type.
% tk_getOpenFile -filetypes {{Text {.txt .md}} {All *}}% tk_getOpenFile -filetypes {{Text}}
bad file type "Text", should be "typeName {extension ?extensions ...?} ?{macType ?macTypes ...?}?"
-errorcode NONE-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_getOpenFile -initialdir /nonexistentA folder that does not exist is not an error.
-initialfile NAME
The name of the file that the dialog selects at the start.
The name goes in the field of the name.
% tk_getOpenFile -initialfile notes.txtAny text is a name.
-message TEXT
A text that the dialog of the platform shows over the files.
The option exists with a dialog of the platform. The dialogs of Windows show no message, and the dialog that ztk draws has no such option.
% tk_getOpenFile -message "Select a script to run."Any text is a message.
-multiple BOOLEAN
Lets the user choose more than one file.
With a true value the result is a list of paths, also for one file.
% tk_getOpenFile -multiple 1% tk_getOpenFile -multiple some
expected boolean value but got "some"
-errorcode NONE-parent WINDOW
The window that the dialog belongs to.
The dialog shows over the toplevel of WINDOW. The window must exist.
% tk_getOpenFile -parent .% tk_getOpenFile -parent .nope
bad window path name ".nope"
-errorcode NONE-title TEXT
The title of the window of the dialog.
The default is Open, in the language of msgcat for the dialog that ztk draws.
% tk_getOpenFile -title "Open a picture"% tk_getOpenFile -title
value for "-title" missing
-errorcode NONE-typevariable VARNAME
A global variable with the name of the file type of the dialog.
When the variable has the name of a type of -filetypes, the dialog shows that type at the start. After the user chooses a file, the variable gets the name of the type that the dialog showed. A cancel leaves the variable as it is.
# The cancel of the headless backend sets no variable.
% unset -nocomplain ::kind
> tk_getOpenFile -typevariable ::kind -filetypes {{Text .txt} {All *}}
> info exists ::kind
0Any text is a name of a variable.
Returns
The path of the file, a list of paths with -multiple 1, or an empty string after a cancel.
Examples
# The headless backend answers with a cancel.
% tk_getOpenFile -title "Open a script" -filetypes {{Tcl .tcl} {All *}}
Errors
# The list of the headless backend, which has the options of a dialog of the platform.
% tk_getOpenFile -name a.txt
bad option "-name": must be -defaultextension, -filetypes, -initialdir, -initialfile, -message, -multiple, -parent, -title, or -typevariable
-errorcode NONE
% tk_getOpenFile -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 dialog that ztk draws has no
-messageoption. - The errors have the
-errorcodeNONE. Tk 8.6.16 gives codes such asTK LOOKUP OPTIONandTK VALUE FILE_TYPE.
Facts
- Capability
- None.
- Tcl release
- Tcl 9.1 and 8.6.
- Since
- ztk 1.0.0-beta.1