1tk_getOpenFile(n) Tk Built-In Commands tk_getOpenFile(n)
2
3
4
5______________________________________________________________________________
6
8 tk_getOpenFile, tk_getSaveFile - pop up a dialog box for the user to
9 select a file to open or save.
10
12 tk_getOpenFile ?option value ...?
13 tk_getSaveFile ?option value ...?
14______________________________________________________________________________
15
17 The procedures tk_getOpenFile and tk_getSaveFile pop up a dialog box
18 for the user to select a file to open or save. The tk_getOpenFile com‐
19 mand is usually associated with the Open command in the File menu. Its
20 purpose is for the user to select an existing file only. If the user
21 enters a non-existent file, the dialog box gives the user an error
22 prompt and requires the user to give an alternative selection. If an
23 application allows the user to create new files, it should do so by
24 providing a separate New menu command.
25
26 The tk_getSaveFile command is usually associated with the Save as com‐
27 mand in the File menu. If the user enters a file that already exists,
28 the dialog box prompts the user for confirmation whether the existing
29 file should be overwritten or not.
30
31 The following option-value pairs are possible as command line arguments
32 to these two commands:
33
34 -command string
35 Specifies the prefix of a Tcl command to invoke when the user
36 closes the dialog after having selected an item. This callback
37 is not called if the user cancelled the dialog. The actual com‐
38 mand consists of string followed by a space and the value
39 selected by the user in the dialog. This is only available on
40 Mac OS X.
41
42 -confirmoverwrite boolean
43 Configures how the Save dialog reacts when the selected file
44 already exists, and saving would overwrite it. A true value
45 requests a confirmation dialog be presented to the user. A
46 false value requests that the overwrite take place without con‐
47 firmation. Default value is true.
48
49 -defaultextension extension
50 Specifies a string that will be appended to the filename if the
51 user enters a filename without an extension. The default value
52 is the empty string, which means no extension will be appended
53 to the filename in any case. This option is ignored on Mac OS X,
54 which does not require extensions to filenames, and the UNIX
55 implementation guesses reasonable values for this from the
56 -filetypes option when this is not supplied.
57
58 -filetypes filePatternList
59 If a File types listbox exists in the file dialog on the partic‐
60 ular platform, this option gives the filetypes in this listbox.
61 When the user choose a filetype in the listbox, only the files
62 of that type are listed. If this option is unspecified, or if it
63 is set to the empty list, or if the File types listbox is not
64 supported by the particular platform then all files are listed
65 regardless of their types. See the section SPECIFYING FILE PAT‐
66 TERNS below for a discussion on the contents of filePatternList.
67
68 -initialdir directory
69 Specifies that the files in directory should be displayed when
70 the dialog pops up. If this parameter is not specified, the ini‐
71 tial directory defaults to the current working directory on non-
72 Windows systems and on Windows systems prior to Vista. On Vista
73 and later systems, the initial directory defaults to the last
74 user-selected directory for the application. If the parameter
75 specifies a relative path, the return value will convert the
76 relative path to an absolute path.
77
78 -initialfile filename
79 Specifies a filename to be displayed in the dialog when it pops
80 up.
81
82 -message string
83 Specifies a message to include in the client area of the dialog.
84 This is only available on Mac OS X.
85
86 -multiple boolean
87 Allows the user to choose multiple files from the Open dialog.
88
89 -parent window
90 Makes window the logical parent of the file dialog. The file
91 dialog is displayed on top of its parent window. On Mac OS X,
92 this turns the file dialog into a sheet attached to the parent
93 window.
94
95 -title titleString
96 Specifies a string to display as the title of the dialog box. If
97 this option is not specified, then a default title is displayed.
98
99 -typevariable variableName
100 The global variable variableName is used to preselect which fil‐
101 ter is used from filterList when the dialog box is opened and is
102 updated when the dialog box is closed, to the last selected fil‐
103 ter. The variable is read once at the beginning to select the
104 appropriate filter. If the variable does not exist, or its value
105 does not match any filter typename, or is empty ({}), the dialog
106 box will revert to the default behavior of selecting the first
107 filter in the list. If the dialog is canceled, the variable is
108 not modified.
109
110 If the user selects a file, both tk_getOpenFile and tk_getSaveFile
111 return the full pathname of this file. If the user cancels the opera‐
112 tion, both commands return the empty string.
113
115 The filePatternList value given by the -filetypes option is a list of
116 file patterns. Each file pattern is a list of the form
117 typeName {extension ?extension ...?} ?{macType ?macType ...?}?
118 typeName is the name of the file type described by this file pattern
119 and is the text string that appears in the File types listbox. exten‐
120 sion is a file extension for this file pattern. macType is a four-
121 character Macintosh file type. The list of macTypes is optional and may
122 be omitted for applications that do not need to execute on the Macin‐
123 tosh platform.
124
125 Several file patterns may have the same typeName, in which case they
126 refer to the same file type and share the same entry in the listbox.
127 When the user selects an entry in the listbox, all the files that match
128 at least one of the file patterns corresponding to that entry are
129 listed. Usually, each file pattern corresponds to a distinct type of
130 file. The use of more than one file pattern for one type of file is
131 only necessary on the Macintosh platform.
132
133 On the Macintosh platform, a file matches a file pattern if its name
134 matches at least one of the extension(s) AND it belongs to at least one
135 of the macType(s) of the file pattern. For example, the C Source Files
136 file pattern in the sample code matches with files that have a .c
137 extension AND belong to the macType TEXT. To use the OR rule instead,
138 you can use two file patterns, one with the extensions only and the
139 other with the macType only. The GIF Files file type in the sample code
140 matches files that either have a .gif extension OR belong to the mac‐
141 Type GIFF.
142
143 On the Unix and Windows platforms, a file matches a file pattern if its
144 name matches at least one of the extension(s) of the file pattern. The
145 macTypes are ignored.
146
148 On the Unix and Macintosh platforms, extensions are matched using glob-
149 style pattern matching. On the Windows platform, extensions are matched
150 by the underlying operating system. The types of possible extensions
151 are:
152
153 (1) the special extension “*” matches any file;
154
155 (2) the special extension “” matches any files that do not have an
156 extension (i.e., the filename contains no full stop character);
157
158 (3) any character string that does not contain any wild card charac‐
159 ters (* and ?).
160
161 Due to the different pattern matching rules on the various platforms,
162 to ensure portability, wild card characters are not allowed in the
163 extensions, except as in the special extension “*”. Extensions without
164 a full stop character (e.g. “~”) are allowed but may not work on all
165 platforms.
166
168 set types {
169 {{Text Files} {.txt} }
170 {{TCL Scripts} {.tcl} }
171 {{C Source Files} {.c} TEXT}
172 {{GIF Files} {.gif} }
173 {{GIF Files} {} GIFF}
174 {{All Files} * }
175 }
176 set filename [tk_getOpenFile -filetypes $types]
177
178 if {$filename ne ""} {
179 # Open the file ...
180 }
181
183 tk_chooseDirectory
184
186 file selection dialog
187
188
189
190Tk 4.2 tk_getOpenFile(n)