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 -defaultextension extension
35 Specifies a string that will be appended to the filename if the
36 user enters a filename without an extension. The default value
37 is the empty string, which means no extension will be appended
38 to the filename in any case. This option is ignored on Mac OS X,
39 which does not require extensions to filenames, and the UNIX
40 implementation guesses reasonable values for this from the
41 -filetypes option when this is not supplied.
42
43 -filetypes filePatternList
44 If a File types listbox exists in the file dialog on the partic‐
45 ular platform, this option gives the filetypes in this listbox.
46 When the user choose a filetype in the listbox, only the files
47 of that type are listed. If this option is unspecified, or if it
48 is set to the empty list, or if the File types listbox is not
49 supported by the particular platform then all files are listed
50 regardless of their types. See the section SPECIFYING FILE PAT‐
51 TERNS below for a discussion on the contents of filePatternList.
52
53 -initialdir directory
54 Specifies that the files in directory should be displayed when
55 the dialog pops up. If this parameter is not specified, then the
56 files in the current working directory are displayed. If the
57 parameter specifies a relative path, the return value will con‐
58 vert the relative path to an absolute path.
59
60 -initialfile filename
61 Specifies a filename to be displayed in the dialog when it pops
62 up.
63
64 -message string
65 Specifies a message to include in the client area of the dialog.
66 This is only available on Mac OS X.
67
68 -multiple boolean
69 Allows the user to choose multiple files from the Open dialog.
70
71 -parent window
72 Makes window the logical parent of the file dialog. The file
73 dialog is displayed on top of its parent window. On Mac OS X,
74 this turns the file dialog into a sheet attached to the parent
75 window.
76
77 -title titleString
78 Specifies a string to display as the title of the dialog box. If
79 this option is not specified, then a default title is displayed.
80
81 -typevariable variableName
82 The global variable variableName is used to preselect which fil‐
83 ter is used from filterList when the dialog box is opened and is
84 updated when the dialog box is closed, to the last selected fil‐
85 ter. The variable is read once at the beginning to select the
86 appropriate filter. If the variable does not exist, or its value
87 does not match any filter typename, or is empty ({}), the dialog
88 box will revert to the default behavior of selecting the first
89 filter in the list. If the dialog is canceled, the variable is
90 not modified.
91
92 If the user selects a file, both tk_getOpenFile and tk_getSaveFile
93 return the full pathname of this file. If the user cancels the opera‐
94 tion, both commands return the empty string.
95
97 The filePatternList value given by the -filetypes option is a list of
98 file patterns. Each file pattern is a list of the form
99 typeName {extension ?extension ...?} ?{macType ?macType ...?}?
100 typeName is the name of the file type described by this file pattern
101 and is the text string that appears in the File types listbox. exten‐
102 sion is a file extension for this file pattern. macType is a four-
103 character Macintosh file type. The list of macTypes is optional and may
104 be omitted for applications that do not need to execute on the Macin‐
105 tosh platform.
106
107 Several file patterns may have the same typeName, in which case they
108 refer to the same file type and share the same entry in the listbox.
109 When the user selects an entry in the listbox, all the files that match
110 at least one of the file patterns corresponding to that entry are
111 listed. Usually, each file pattern corresponds to a distinct type of
112 file. The use of more than one file pattern for one type of file is
113 only necessary on the Macintosh platform.
114
115 On the Macintosh platform, a file matches a file pattern if its name
116 matches at least one of the extension(s) AND it belongs to at least one
117 of the macType(s) of the file pattern. For example, the C Source Files
118 file pattern in the sample code matches with files that have a .c
119 extension AND belong to the macType TEXT. To use the OR rule instead,
120 you can use two file patterns, one with the extensions only and the
121 other with the macType only. The GIF Files file type in the sample code
122 matches files that either have a .gif extension OR belong to the mac‐
123 Type GIFF.
124
125 On the Unix and Windows platforms, a file matches a file pattern if its
126 name matches at least one of the extension(s) of the file pattern. The
127 macTypes are ignored.
128
130 On the Unix and Macintosh platforms, extensions are matched using glob-
131 style pattern matching. On the Windows platform, extensions are matched
132 by the underlying operating system. The types of possible extensions
133 are:
134
135 (1) the special extension “*” matches any file;
136
137 (2) the special extension matches any files that do not have an
138 extension (i.e., the filename contains no full stop character);
139
140 (3) any character string that does not contain any wild card
141 characters (* and ?).
142
143 Due to the different pattern matching rules on the various platforms,
144 to ensure portability, wild card characters are not allowed in the
145 extensions, except as in the special extension “*”. Extensions without
146 a full stop character (e.g. “~”) are allowed but may not work on all
147 platforms.
148
150 set types {
151 {{Text Files} {.txt} }
152 {{TCL Scripts} {.tcl} }
153 {{C Source Files} {.c} TEXT}
154 {{GIF Files} {.gif} }
155 {{GIF Files} {} GIFF}
156 {{All Files} * }
157 }
158 set filename [tk_getOpenFile -filetypes $types]
159
160 if {$filename != ""} {
161 # Open the file ...
162 }
163
165 tk_chooseDirectory
166
168 file selection dialog
169
170
171
172Tk 4.2 tk_getOpenFile(n)