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
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
23 # - initialdir: initial directory. preserved by dialog instance.
25 # - initialfile: initial file (ignored by the open dialog). preserved
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
):
49 # make sure "filetypes" is a tuple
50 self
.options
["filetypes"] = tuple(self
.options
["filetypes"])
54 def _fixresult(self
, widget
, result
):
56 # keep directory and filename until next time
58 # convert Tcl path objects to strings
60 result
= result
.string
61 except AttributeError:
62 # it already is a string
64 path
, file = os
.path
.split(result
)
65 self
.options
["initialdir"] = path
66 self
.options
["initialfile"] = file
67 self
.filename
= result
# compatibility
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
):
89 command
= "tk_chooseDirectory"
91 def _fixresult(self
, widget
, result
):
93 # keep directory until next time
94 self
.options
["initialdir"] = result
95 self
.directory
= result
# compatibility
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()
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()
129 return open(filename
, mode
)
132 def askopenfiles(mode
= "r", **options
):
133 """Ask for multiple filenames and return the open file
136 returns a list of open file objects or an empty list if
140 files
= askopenfilenames(**options
)
143 for filename
in files
:
144 ofiles
.append(open(filename
, mode
))
149 def asksaveasfile(mode
= "w", **options
):
150 "Ask for a filename to save as, and returned the opened file"
152 filename
= SaveAs(**options
).show()
154 return open(filename
, mode
)
157 def askdirectory (**options
):
158 "Ask for a directory, and return the file name"
159 return Directory(**options
).show()
161 # --------------------------------------------------------------------
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
173 # See whether CODESET is defined
176 locale
.setlocale(locale
.LC_ALL
,'')
177 enc
= locale
.nl_langinfo(locale
.CODESET
)
178 except (ImportError, AttributeError):
181 # dialog for openening files
183 openfilename
=askopenfilename(filetypes
=[("all files", "*")])
185 fp
=open(openfilename
,"r")
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
)