Issue #3809: Fixed spurious 'test.blah' file left behind by test_logging.
[python.git] / Lib / lib-tk / tkFileDialog.py
blob15c7d5f606d9347a05faa16e61c2ba8821e28a3c
2 # Instant Python
3 # $Id: tkFileDialog.py 36560 2004-07-18 06:16:08Z tim_one $
5 # tk common file dialogues
7 # this module provides interfaces to the native file dialogues
8 # available in Tk 4.2 and newer, and the directory dialogue available
9 # in Tk 8.3 and newer.
11 # written by Fredrik Lundh, May 1997.
15 # options (all have default values):
17 # - defaultextension: added to filename if not explicitly given
19 # - filetypes: sequence of (label, pattern) tuples. the same pattern
20 # may occur with several patterns. use "*" as pattern to indicate
21 # all files.
23 # - initialdir: initial directory. preserved by dialog instance.
25 # - initialfile: initial file (ignored by the open dialog). preserved
26 # by dialog instance.
28 # - parent: which window to place the dialog on top of
30 # - title: dialog title
32 # - multiple: if true user may select more than one file
34 # options for the directory chooser:
36 # - initialdir, parent, title: see above
38 # - mustexist: if true, user must pick an existing directory
43 from tkCommonDialog import Dialog
45 class _Dialog(Dialog):
47 def _fixoptions(self):
48 try:
49 # make sure "filetypes" is a tuple
50 self.options["filetypes"] = tuple(self.options["filetypes"])
51 except KeyError:
52 pass
54 def _fixresult(self, widget, result):
55 if result:
56 # keep directory and filename until next time
57 import os
58 # convert Tcl path objects to strings
59 try:
60 result = result.string
61 except AttributeError:
62 # it already is a string
63 pass
64 path, file = os.path.split(result)
65 self.options["initialdir"] = path
66 self.options["initialfile"] = file
67 self.filename = result # compatibility
68 return result
72 # file dialogs
74 class Open(_Dialog):
75 "Ask for a filename to open"
77 command = "tk_getOpenFile"
79 def _fixresult(self, widget, result):
80 if isinstance(result, tuple):
81 # multiple results:
82 result = tuple([getattr(r, "string", r) for r in result])
83 if result:
84 import os
85 path, file = os.path.split(result[0])
86 self.options["initialdir"] = path
87 # don't set initialfile or filename, as we have multiple of these
88 return result
89 if not widget.tk.wantobjects() and "multiple" in self.options:
90 # Need to split result explicitly
91 return self._fixresult(widget, widget.tk.splitlist(result))
92 return _Dialog._fixresult(self, widget, result)
94 class SaveAs(_Dialog):
95 "Ask for a filename to save as"
97 command = "tk_getSaveFile"
100 # the directory dialog has its own _fix routines.
101 class Directory(Dialog):
102 "Ask for a directory"
104 command = "tk_chooseDirectory"
106 def _fixresult(self, widget, result):
107 if result:
108 # convert Tcl path objects to strings
109 try:
110 result = result.string
111 except AttributeError:
112 # it already is a string
113 pass
114 # keep directory until next time
115 self.options["initialdir"] = result
116 self.directory = result # compatibility
117 return result
120 # convenience stuff
122 def askopenfilename(**options):
123 "Ask for a filename to open"
125 return Open(**options).show()
127 def asksaveasfilename(**options):
128 "Ask for a filename to save as"
130 return SaveAs(**options).show()
132 def askopenfilenames(**options):
133 """Ask for multiple filenames to open
135 Returns a list of filenames or empty list if
136 cancel button selected
138 options["multiple"]=1
139 return Open(**options).show()
141 # FIXME: are the following perhaps a bit too convenient?
143 def askopenfile(mode = "r", **options):
144 "Ask for a filename to open, and returned the opened file"
146 filename = Open(**options).show()
147 if filename:
148 return open(filename, mode)
149 return None
151 def askopenfiles(mode = "r", **options):
152 """Ask for multiple filenames and return the open file
153 objects
155 returns a list of open file objects or an empty list if
156 cancel selected
159 files = askopenfilenames(**options)
160 if files:
161 ofiles=[]
162 for filename in files:
163 ofiles.append(open(filename, mode))
164 files=ofiles
165 return files
168 def asksaveasfile(mode = "w", **options):
169 "Ask for a filename to save as, and returned the opened file"
171 filename = SaveAs(**options).show()
172 if filename:
173 return open(filename, mode)
174 return None
176 def askdirectory (**options):
177 "Ask for a directory, and return the file name"
178 return Directory(**options).show()
180 # --------------------------------------------------------------------
181 # test stuff
183 if __name__ == "__main__":
184 # Since the file name may contain non-ASCII characters, we need
185 # to find an encoding that likely supports the file name, and
186 # displays correctly on the terminal.
188 # Start off with UTF-8
189 enc = "utf-8"
190 import sys
192 # See whether CODESET is defined
193 try:
194 import locale
195 locale.setlocale(locale.LC_ALL,'')
196 enc = locale.nl_langinfo(locale.CODESET)
197 except (ImportError, AttributeError):
198 pass
200 # dialog for openening files
202 openfilename=askopenfilename(filetypes=[("all files", "*")])
203 try:
204 fp=open(openfilename,"r")
205 fp.close()
206 except:
207 print "Could not open File: "
208 print sys.exc_info()[1]
210 print "open", openfilename.encode(enc)
212 # dialog for saving files
214 saveasfilename=asksaveasfilename()
215 print "saveas", saveasfilename.encode(enc)