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 path
, file = os
.path
.split(result
)
59 self
.options
["initialdir"] = path
60 self
.options
["initialfile"] = file
61 self
.filename
= result
# compatibility
69 "Ask for a filename to open"
71 command
= "tk_getOpenFile"
73 class SaveAs(_Dialog
):
74 "Ask for a filename to save as"
76 command
= "tk_getSaveFile"
79 # the directory dialog has its own _fix routines.
80 class Directory(Dialog
):
83 command
= "tk_chooseDirectory"
85 def _fixresult(self
, widget
, result
):
87 # keep directory until next time
88 self
.options
["initialdir"] = result
89 self
.directory
= result
# compatibility
95 def askopenfilename(**options
):
96 "Ask for a filename to open"
98 return Open(**options
).show()
100 def asksaveasfilename(**options
):
101 "Ask for a filename to save as"
103 return SaveAs(**options
).show()
105 def askopenfilenames(**options
):
106 """Ask for multiple filenames to open
108 Returns a list of filenames or empty list if
109 cancel button selected
111 options
["multiple"]=1
112 files
=Open(**options
).show()
116 # FIXME: are the following perhaps a bit too convenient?
118 def askopenfile(mode
= "r", **options
):
119 "Ask for a filename to open, and returned the opened file"
121 filename
= Open(**options
).show()
123 return open(filename
, mode
)
126 def askopenfiles(mode
= "r", **options
):
127 """Ask for multiple filenames and return the open file
130 returns a list of open file objects or an empty list if
134 files
= askopenfilenames(**options
)
137 for filename
in files
:
138 ofiles
.append(open(filename
, mode
))
143 def asksaveasfile(mode
= "w", **options
):
144 "Ask for a filename to save as, and returned the opened file"
146 filename
= SaveAs(**options
).show()
148 return open(filename
, mode
)
151 def askdirectory (**options
):
152 "Ask for a directory, and return the file name"
153 return Directory(**options
).show()
155 # --------------------------------------------------------------------
158 if __name__
== "__main__":
159 # Since the file name may contain non-ASCII characters, we need
160 # to find an encoding that likely supports the file name, and
161 # displays correctly on the terminal.
163 # Start off with UTF-8
167 # See whether CODESET is defined
170 locale
.setlocale(locale
.LC_ALL
,'')
171 enc
= locale
.nl_langinfo(locale
.CODESET
)
172 except (ImportError, AttributeError):
175 # dialog for openening files
177 openfilename
=askopenfilename(filetypes
=[("all files", "*")])
179 fp
=open(openfilename
,"r")
182 print "Could not open File: "
183 print sys
.exc_info()[1]
185 print "open", openfilename
.encode(enc
)
187 # dialog for saving files
189 saveasfilename
=asksaveasfilename()
190 print "saveas", saveasfilename
.encode(enc
)