tk_getOpenFile(3tk)
β¨ tk_getOpenFile β bestie edition π
okay so we're talking about tk_getOpenFile and tk_getSaveFile, which live in the Tk Built-In Commands squad. they pop up a dialog box so your user can pick a file to open or save. iconic behavior, truly.
πΈ SYNOPSIS πΈ
tk_getOpenFile ?option value ...?
tk_getSaveFile ?option value ...?
π¦ DESCRIPTION π¦
so tk_getOpenFile and tk_getSaveFile both pop up a dialog box for the user to select a file to open or save. that's the whole vibe. let's break it down tho because they are NOT the same girl.
tk_getOpenFile is usually the one attached to the Open command in the File menu. its whole purpose is letting the user select an existing file, only. if the user types in a file that does not exist, the dialog box gives them an error prompt and makes them pick something else. periodt. so if your app wants to let people create new files, do that separately with a New menu command β don't try to make this dialog do it, she won't.
tk_getSaveFile is usually attached to the Save as command in the File menu. and here's the thing: if the user enters a file that already exists, the dialog box prompts them for confirmation on whether the existing file should be overwritten or not. very "are you sure bestie" of it.
the following option-value pairs are possible as command line arguments to these two commands:
-commandstring β specifies the prefix of a Tcl command to invoke when the user closes the dialog after having selected an item. this callback is not called if the user cancelled the dialog. the actual command is string followed by a space and the value the user selected in the dialog. this is only available on macOS.-confirmoverwriteboolean β configures how the Save dialog reacts when the selected file already exists and saving would overwrite it. a true value asks for a confirmation dialog to be presented to the user. a false value requests the overwrite happen with no confirmation. default value istrue.-defaultextensionextension β a string that gets appended to the filename if the user enters a filename without an extension. default value is the empty string, which means no extension will be appended in any case. this option is ignored on macOS, which doesn't require extensions to filenames, and the UNIX implementation guesses reasonable values for this from the-filetypesoption when this isn't supplied.-filetypesfilePatternList β if a File types listbox exists in the file dialog on the particular platform, this option gives the filetypes in that listbox. when the user chooses a filetype in the listbox, only files of that type are listed. if this option is unspecified, or set to the empty list, or if the File types listbox isn't supported by the particular platform, then all files are listed regardless of their types. see the section SPECIFYING FILE PATTERNS below for the discussion on what goes inside filePatternList. π«Ά-initialdirdirectory β specifies that the files in directory should be displayed when the dialog pops up. if this parameter isn't specified, the initial directory defaults to the current working directory on non-Windows systems and on Windows systems prior to Vista. on Vista and later systems, the initial directory defaults to the last user-selected directory for the application. if the parameter specifies a relative path, the return value will convert that relative path into an absolute path.-initialfilefilename β specifies a filename to be displayed in the dialog when it pops up.-messagestring β specifies a message to include in the client area of the dialog. this is only available on macOS.-multipleboolean β allows the user to choose multiple files from the Open dialog. (go off, honestly.)-parentwindow β makes window the logical parent of the file dialog. the file dialog is displayed on top of its parent window. on macOS, this turns the file dialog into a sheet attached to the parent window.-titletitleString β a string to display as the title of the dialog box. if this option isn't specified, then a default title is displayed.-typevariablevariableName β the global variable variableName is used to preselect which filter is used from filterList when the dialog box is opened, and is updated when the dialog box is closed to the last selected filter. the variable is read once at the beginning to select the appropriate filter. if the variable doesn't exist, or its value doesn't match any filter typename, or is empty ({}), the dialog box reverts to the default behavior of selecting the first filter in the list. if the dialog is canceled, the variable is not modified.
so if the user selects a file, both tk_getOpenFile and tk_getSaveFile return the full pathname of that file. if the user cancels the operation, both commands return the empty string. that's the tea.
π SPECIFYING FILE PATTERNS π
the filePatternList value given by the -filetypes option is a list of file patterns. each file pattern is a list of the form:
typeName {extension ?extension ...?} ?{macType ?macType ...?}?
typeName is the name of the file type described by this file pattern, and it's the text string that appears in the File types listbox. extension is a file extension for this file pattern. macType is a four-character Macintosh file type. the list of *macType*s is optional and may be omitted for applications that don't need to execute on the Macintosh platform.
several file patterns may have the same typeName, in which case they refer to the same file type and share the same entry in the listbox. when the user selects an entry in the listbox, all the files that match at least one of the file patterns corresponding to that entry are listed. usually each file pattern corresponds to a distinct type of file. using more than one file pattern for one type of file is only necessary on the Macintosh platform.
on the Macintosh platform, a file matches a file pattern if its name matches at least one of the extension(s) AND it belongs to at least one of the macType(s) of the file pattern. for example, the C Source Files file pattern in the sample code matches files that have a .c extension AND belong to the macType TEXT. to use the OR rule instead, you can use two file patterns, one with the extensions only and the other with the macType only. the GIF Files file type in the sample code matches files that either have a .gif extension OR belong to the macType GIFF.
on the Unix and Windows platforms, a file matches a file pattern if its name matches at least one of the extension(s) of the file pattern. the *macType*s are ignored.
π SPECIFYING EXTENSIONS π
on the Unix and Macintosh platforms, extensions are matched using glob-style pattern matching. on the Windows platform, extensions are matched by the underlying operating system. the types of possible extensions are:
- the special extension
"*"matches any file; - the special extension
""matches any files that do not have an extension (i.e., the filename contains no full stop character); - any character string that does not contain any wild card characters (
*and?).
because the pattern matching rules differ across the various platforms, to ensure portability, wild card characters are not allowed in the extensions, except as in the special extension "*". extensions without a full stop character (e.g. "~") are allowed but may not work on all platforms. so likeβ¦ be careful bestie. π
β EXAMPLE β
set types {
{{Text Files} {.txt} }
{{TCL Scripts} {.tcl} }
{{C Source Files} {.c} TEXT}
{{GIF Files} {.gif} }
{{GIF Files} {} GIFF}
{{All Files} * }
}
set filename [tk_getOpenFile -filetypes $types]
if {$filename ne ""} {
# Open the file ...
}
π SEE ALSO π
tk_chooseDirectory
π KEYWORDS π
file selection dialog
okay that's the whole page, no notes. this gem is from the Tk manual, version 4.2, and it's been serving file-dialog realness since forever. slay, go pick some files, mwah πππ¦
π¦ where this page comes from
- debian trixie: tk8.6-doc 8.6.16-1
- debian trixie: tk9.0-doc 9.0.1-3
- ubuntu questing: tk8.6-doc 8.6.17-1
- ubuntu questing: tk9.0-doc 9.0.2-1
- ubuntu resolute: tk8.6-doc 8.6.17-1build1
- ubuntu resolute: tk9.0-doc 9.0.3-1
- ubuntu stonking: tk8.6-doc 8.6.18-1
- ubuntu stonking: tk9.0-doc 9.0.4-1
- Copyright (c) 1996 Sun Microsystems, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms