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
16
18 The procedures tk_getOpenFile and tk_getSaveFile pop up a dialog box
19 for the user to select a file to open or save. The tk_getOpenFile com‐
20 mand is usually associated with the Open command in the File menu. Its
21 purpose is for the user to select an existing file only. If the user
22 enters a non-existent file, the dialog box gives the user an error
23 prompt and requires the user to give an alternative selection. If an
24 application allows the user to create new files, it should do so by
25 providing a separate New menu command.
26
27 The tk_getSaveFile command is usually associated with the Save as com‐
28 mand in the File menu. If the user enters a file that already exists,
29 the dialog box prompts the user for confirmation whether the existing
30 file should be overwritten or not.
31
32 The following option-value pairs are possible as command line arguments
33 to these two commands:
34
35 -defaultextension extension
36 Specifies a string that will be appended to the filename if the
37 user enters a filename without an extension. The default value
38 is the empty string, which means no extension will be appended
39 to the filename in any case. This option is ignored on the Mac‐
40 intosh platform, which does not require extensions to filenames, │
41 and the UNIX implementation guesses reasonable values for this │
42 from the -filetypes option when this is not supplied.
43
44 -filetypes filePatternList
45 If a File types listbox exists in the file dialog on the partic‐
46 ular platform, this option gives the filetypes in this listbox.
47 When the user choose a filetype in the listbox, only the files
48 of that type are listed. If this option is unspecified, or if it
49 is set to the empty list, or if the File types listbox is not
50 supported by the particular platform then all files are listed
51 regardless of their types. See the section SPECIFYING FILE PAT‐
52 TERNS below for a discussion on the contents of filePatternList.
53
54 -initialdir directory
55 Specifies that the files in directory should be displayed when
56 the dialog pops up. If this parameter is not specified, then the
57 files in the current working directory are displayed. If the
58 parameter specifies a relative path, the return value will con‐
59 vert the relative path to an absolute path. This option may not
60 always work on the Macintosh. This is not a bug. Rather, the
61 General Controls control panel on the Mac allows the end user to
62 override the application default directory.
63
64 -initialfile filename
65 Specifies a filename to be displayed in the dialog when it pops
66 up. This option is ignored on the Macintosh platform.
67
68 -multiple boolean
69 Allows the user to choose multiple files from the Open dialog.
70 On the Macintosh, this is only available when Navigation Ser‐
71 vices are installed.
72
73 -message string
74 Specifies a message to include in the client area of the dialog.
75 This is only available on the Macintosh, and only when Naviga‐
76 tion Services are installed.
77
78 -parent window
79 Makes window the logical parent of the file dialog. The file
80 dialog is displayed on top of its parent window.
81
82 -title titleString
83 Specifies a string to display as the title of the dialog box. If
84 this option is not specified, then a default title is displayed.
85
86 If the user selects a file, both tk_getOpenFile and tk_getSaveFile
87 return the full pathname of this file. If the user cancels the opera‐
88 tion, both commands return the empty string.
89
91 The filePatternList value given by the -filetypes option is a list of
92 file patterns. Each file pattern is a list of the form
93 typeName {extension ?extension ...?} ?{macType ?macType ...?}?
94 typeName is the name of the file type described by this file pattern
95 and is the text string that appears in the File types listbox. exten‐
96 sion is a file extension for this file pattern. macType is a four-
97 character Macintosh file type. The list of macTypes is optional and may
98 be omitted for applications that do not need to execute on the Macin‐
99 tosh platform.
100
101 Several file patterns may have the same typeName, in which case they
102 refer to the same file type and share the same entry in the listbox.
103 When the user selects an entry in the listbox, all the files that match
104 at least one of the file patterns corresponding to that entry are
105 listed. Usually, each file pattern corresponds to a distinct type of
106 file. The use of more than one file patterns for one type of file is
107 necessary on the Macintosh platform only.
108
109 On the Macintosh platform, a file matches a file pattern if its name
110 matches at least one of the extension(s) AND it belongs to at least one
111 of the macType(s) of the file pattern. For example, the C Source Files
112 file pattern in the sample code matches with files that have a .c
113 extension AND belong to the macType TEXT. To use the OR rule instead,
114 you can use two file patterns, one with the extensions only and the
115 other with the macType only. The GIF Files file type in the sample code
116 matches files that EITHER have a .gif extension OR belong to the mac‐
117 Type GIFF.
118
119 On the Unix and Windows platforms, a file matches a file pattern if its
120 name matches at least one of the extension(s) of the file pattern. The
121 macTypes are ignored.
122
124 On the Unix and Macintosh platforms, extensions are matched using glob-
125 style pattern matching. On the Windows platforms, extensions are
126 matched by the underlying operating system. The types of possible
127 extensions are: (1) the special extension * matches any file; (2) the
128 special extension "" matches any files that do not have an extension
129 (i.e., the filename contains no full stop character); (3) any character
130 string that does not contain any wild card characters (* and ?).
131
132 Due to the different pattern matching rules on the various platforms,
133 to ensure portability, wild card characters are not allowed in the
134 extensions, except as in the special extension *. Extensions without a
135 full stop character (e.g. ~) are allowed but may not work on all plat‐
136 forms.
137
138
140 set types {
141 {{Text Files} {.txt} }
142 {{TCL Scripts} {.tcl} }
143 {{C Source Files} {.c} TEXT}
144 {{GIF Files} {.gif} }
145 {{GIF Files} {} GIFF}
146 {{All Files} * }
147 }
148 set filename [tk_getOpenFile -filetypes $types]
149
150 if {$filename != ""} {
151 # Open the file ...
152 }
153
154
156 tk_chooseDirectory
157
158
160 file selection dialog
161
162
163
164Tk 4.2 tk_getOpenFile(n)