beetlebug▾
/

tk_getOpenFile

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:

-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

OptionTypeDefaultDescription
-defaultextensionstringemptyThe extension of a name with none, for the file dialogs of Tk that save.
-filetypeslistemptyThe filters of the dialog: a list of file types, each a name and its patterns.
-initialdirpathemptyThe folder that the dialog shows at the start.
-initialfilestringemptyThe name of the file that the dialog selects at the start.
-messagestringemptyA text that the dialog of the platform shows over the files.
-multipleboolean0Lets the user choose more than one file.
-parentwindow.The window that the dialog belongs to.
-titlestringnoneThe title of the window of the dialog.
-typevariablevariable nameemptyA 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 .txt

Any 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 /nonexistent

A 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.txt

Any 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
0

Any 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

Facts

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

See also