blob: a35feedc50e3c9151598667be60bc62dfb85cb61 [file] [log] [blame]
Guido van Rossum1e8c8a21997-07-19 20:02:36 +00001#
2# Instant Python
3# $Id$
4#
5# tk common file dialogues
6#
7# this module provides interfaces to the native file dialogues
Martin v. Löwisbc0ad2d2001-11-08 17:51:33 +00008# available in Tk 4.2 and newer, and the directory dialogue available
9# in Tk 8.3 and newer.
Guido van Rossum1e8c8a21997-07-19 20:02:36 +000010#
11# written by Fredrik Lundh, May 1997.
12#
13
14#
15# options (all have default values):
16#
17# - defaultextension: added to filename if not explicitly given
18#
19# - filetypes: sequence of (label, pattern) tuples. the same pattern
20# may occur with several patterns. use "*" as pattern to indicate
21# all files.
22#
23# - initialdir: initial directory. preserved by dialog instance.
24#
25# - initialfile: initial file (ignored by the open dialog). preserved
26# by dialog instance.
27#
28# - parent: which window to place the dialog on top of
29#
30# - title: dialog title
31#
Martin v. Löwisb24e3472002-10-13 10:28:04 +000032# - multiple: if true user may select more than one file
33#
Martin v. Löwisbc0ad2d2001-11-08 17:51:33 +000034# options for the directory chooser:
35#
36# - initialdir, parent, title: see above
37#
38# - mustexist: if true, user must pick an existing directory
39#
Martin v. Löwisb24e3472002-10-13 10:28:04 +000040#
41
Guido van Rossum1e8c8a21997-07-19 20:02:36 +000042
43from tkCommonDialog import Dialog
44
45class _Dialog(Dialog):
46
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
53
54 def _fixresult(self, widget, result):
Guido van Rossumc4570481998-03-20 20:45:49 +000055 if result:
56 # keep directory and filename until next time
Guido van Rossum1e8c8a21997-07-19 20:02:36 +000057 import os
Martin v. Löwis25c7b502003-01-04 00:08:09 +000058 # convert Tcl path objects to strings
59 try:
60 result = result.string
61 except AttributeError:
62 # it already is a string
63 pass
Guido van Rossumc4570481998-03-20 20:45:49 +000064 path, file = os.path.split(result)
65 self.options["initialdir"] = path
66 self.options["initialfile"] = file
Guido van Rossum1e8c8a21997-07-19 20:02:36 +000067 self.filename = result # compatibility
Guido van Rossumc4570481998-03-20 20:45:49 +000068 return result
Guido van Rossum1e8c8a21997-07-19 20:02:36 +000069
70
71#
72# file dialogs
73
74class Open(_Dialog):
75 "Ask for a filename to open"
76
77 command = "tk_getOpenFile"
78
Martin v. Löwisd6efae52003-06-14 21:34:32 +000079 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))
Guido van Rossum8b763512003-06-15 19:08:57 +000092 return _Dialog._fixresult(self, widget, result)
Martin v. Löwisd6efae52003-06-14 21:34:32 +000093
Guido van Rossum1e8c8a21997-07-19 20:02:36 +000094class SaveAs(_Dialog):
95 "Ask for a filename to save as"
96
97 command = "tk_getSaveFile"
98
Martin v. Löwisbc0ad2d2001-11-08 17:51:33 +000099
100# the directory dialog has its own _fix routines.
101class Directory(Dialog):
Martin v. Löwis25ee87c2001-11-07 22:38:08 +0000102 "Ask for a directory"
103
104 command = "tk_chooseDirectory"
Guido van Rossum1e8c8a21997-07-19 20:02:36 +0000105
Martin v. Löwisbc0ad2d2001-11-08 17:51:33 +0000106 def _fixresult(self, widget, result):
107 if result:
Martin v. Löwis85f48e32003-12-01 21:04:22 +0000108 # convert Tcl path objects to strings
109 try:
110 result = result.string
111 except AttributeError:
112 # it already is a string
113 pass
Martin v. Löwisbc0ad2d2001-11-08 17:51:33 +0000114 # keep directory until next time
115 self.options["initialdir"] = result
116 self.directory = result # compatibility
117 return result
118
Guido van Rossum1e8c8a21997-07-19 20:02:36 +0000119#
120# convenience stuff
121
122def askopenfilename(**options):
123 "Ask for a filename to open"
124
Martin v. Löwis25ee87c2001-11-07 22:38:08 +0000125 return Open(**options).show()
Guido van Rossum1e8c8a21997-07-19 20:02:36 +0000126
127def asksaveasfilename(**options):
128 "Ask for a filename to save as"
129
Martin v. Löwis25ee87c2001-11-07 22:38:08 +0000130 return SaveAs(**options).show()
Guido van Rossum1e8c8a21997-07-19 20:02:36 +0000131
Martin v. Löwisb24e3472002-10-13 10:28:04 +0000132def askopenfilenames(**options):
133 """Ask for multiple filenames to open
Tim Peters182b5ac2004-07-18 06:16:08 +0000134
135 Returns a list of filenames or empty list if
Martin v. Löwisb24e3472002-10-13 10:28:04 +0000136 cancel button selected
137 """
138 options["multiple"]=1
Martin v. Löwisd6efae52003-06-14 21:34:32 +0000139 return Open(**options).show()
Martin v. Löwisb24e3472002-10-13 10:28:04 +0000140
141# FIXME: are the following perhaps a bit too convenient?
Guido van Rossum1e8c8a21997-07-19 20:02:36 +0000142
143def askopenfile(mode = "r", **options):
144 "Ask for a filename to open, and returned the opened file"
145
Martin v. Löwis25ee87c2001-11-07 22:38:08 +0000146 filename = Open(**options).show()
Guido van Rossum1e8c8a21997-07-19 20:02:36 +0000147 if filename:
148 return open(filename, mode)
149 return None
150
Martin v. Löwisb24e3472002-10-13 10:28:04 +0000151def askopenfiles(mode = "r", **options):
152 """Ask for multiple filenames and return the open file
153 objects
Tim Peters182b5ac2004-07-18 06:16:08 +0000154
155 returns a list of open file objects or an empty list if
Martin v. Löwisb24e3472002-10-13 10:28:04 +0000156 cancel selected
157 """
158
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
166
167
Guido van Rossum1e8c8a21997-07-19 20:02:36 +0000168def asksaveasfile(mode = "w", **options):
169 "Ask for a filename to save as, and returned the opened file"
170
Martin v. Löwis25ee87c2001-11-07 22:38:08 +0000171 filename = SaveAs(**options).show()
Guido van Rossum1e8c8a21997-07-19 20:02:36 +0000172 if filename:
173 return open(filename, mode)
174 return None
175
Martin v. Löwis25ee87c2001-11-07 22:38:08 +0000176def askdirectory (**options):
177 "Ask for a directory, and return the file name"
178 return Directory(**options).show()
Guido van Rossum1e8c8a21997-07-19 20:02:36 +0000179
180# --------------------------------------------------------------------
181# test stuff
182
183if __name__ == "__main__":
Martin v. Löwis85f98142001-12-30 14:43:56 +0000184 # 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.
Guido van Rossum1e8c8a21997-07-19 20:02:36 +0000187
Martin v. Löwis85f98142001-12-30 14:43:56 +0000188 # Start off with UTF-8
189 enc = "utf-8"
Martin v. Löwis8509ebc2002-04-08 14:51:31 +0000190 import sys
Martin v. Löwis85f98142001-12-30 14:43:56 +0000191
192 # See whether CODESET is defined
193 try:
194 import locale
Martin v. Löwis8509ebc2002-04-08 14:51:31 +0000195 locale.setlocale(locale.LC_ALL,'')
Martin v. Löwis85f98142001-12-30 14:43:56 +0000196 enc = locale.nl_langinfo(locale.CODESET)
197 except (ImportError, AttributeError):
198 pass
199
Martin v. Löwis8509ebc2002-04-08 14:51:31 +0000200 # dialog for openening files
201
202 openfilename=askopenfilename(filetypes=[("all files", "*")])
203 try:
204 fp=open(openfilename,"r")
205 fp.close()
206 except:
Tim Peters182b5ac2004-07-18 06:16:08 +0000207 print "Could not open File: "
Martin v. Löwis8509ebc2002-04-08 14:51:31 +0000208 print sys.exc_info()[1]
209
210 print "open", openfilename.encode(enc)
211
212 # dialog for saving files
213
214 saveasfilename=asksaveasfilename()
215 print "saveas", saveasfilename.encode(enc)