Move setting of ioready 'wait' earlier in call chain, to
[python/dscho.git] / Lib / lib-tk / tkFileDialog.py
blob63487d2037b472089283e0c860bbd827b8695a02
2 # Instant Python
3 # $Id$
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 class SaveAs(_Dialog):
80 "Ask for a filename to save as"
82 command = "tk_getSaveFile"
85 # the directory dialog has its own _fix routines.
86 class Directory(Dialog):
87 "Ask for a directory"
89 command = "tk_chooseDirectory"
91 def _fixresult(self, widget, result):
92 if result:
93 # keep directory until next time
94 self.options["initialdir"] = result
95 self.directory = result # compatibility
96 return result
99 # convenience stuff
101 def askopenfilename(**options):
102 "Ask for a filename to open"
104 return Open(**options).show()
106 def asksaveasfilename(**options):
107 "Ask for a filename to save as"
109 return SaveAs(**options).show()
111 def askopenfilenames(**options):
112 """Ask for multiple filenames to open
114 Returns a list of filenames or empty list if
115 cancel button selected
117 options["multiple"]=1
118 files=Open(**options).show()
119 return files.split()
122 # FIXME: are the following perhaps a bit too convenient?
124 def askopenfile(mode = "r", **options):
125 "Ask for a filename to open, and returned the opened file"
127 filename = Open(**options).show()
128 if filename:
129 return open(filename, mode)
130 return None
132 def askopenfiles(mode = "r", **options):
133 """Ask for multiple filenames and return the open file
134 objects
136 returns a list of open file objects or an empty list if
137 cancel selected
140 files = askopenfilenames(**options)
141 if files:
142 ofiles=[]
143 for filename in files:
144 ofiles.append(open(filename, mode))
145 files=ofiles
146 return files
149 def asksaveasfile(mode = "w", **options):
150 "Ask for a filename to save as, and returned the opened file"
152 filename = SaveAs(**options).show()
153 if filename:
154 return open(filename, mode)
155 return None
157 def askdirectory (**options):
158 "Ask for a directory, and return the file name"
159 return Directory(**options).show()
161 # --------------------------------------------------------------------
162 # test stuff
164 if __name__ == "__main__":
165 # Since the file name may contain non-ASCII characters, we need
166 # to find an encoding that likely supports the file name, and
167 # displays correctly on the terminal.
169 # Start off with UTF-8
170 enc = "utf-8"
171 import sys
173 # See whether CODESET is defined
174 try:
175 import locale
176 locale.setlocale(locale.LC_ALL,'')
177 enc = locale.nl_langinfo(locale.CODESET)
178 except (ImportError, AttributeError):
179 pass
181 # dialog for openening files
183 openfilename=askopenfilename(filetypes=[("all files", "*")])
184 try:
185 fp=open(openfilename,"r")
186 fp.close()
187 except:
188 print "Could not open File: "
189 print sys.exc_info()[1]
191 print "open", openfilename.encode(enc)
193 # dialog for saving files
195 saveasfilename=asksaveasfilename()
196 print "saveas", saveasfilename.encode(enc)