blob: 63487d2037b472089283e0c860bbd827b8695a02 [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
79class SaveAs(_Dialog):
80 "Ask for a filename to save as"
81
82 command = "tk_getSaveFile"
83
Martin v. Löwisbc0ad2d2001-11-08 17:51:33 +000084
85# the directory dialog has its own _fix routines.
86class Directory(Dialog):
Martin v. Löwis25ee87c2001-11-07 22:38:08 +000087 "Ask for a directory"
88
89 command = "tk_chooseDirectory"
Guido van Rossum1e8c8a21997-07-19 20:02:36 +000090
Martin v. Löwisbc0ad2d2001-11-08 17:51:33 +000091 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
97
Guido van Rossum1e8c8a21997-07-19 20:02:36 +000098#
99# convenience stuff
100
101def askopenfilename(**options):
102 "Ask for a filename to open"
103
Martin v. Löwis25ee87c2001-11-07 22:38:08 +0000104 return Open(**options).show()
Guido van Rossum1e8c8a21997-07-19 20:02:36 +0000105
106def asksaveasfilename(**options):
107 "Ask for a filename to save as"
108
Martin v. Löwis25ee87c2001-11-07 22:38:08 +0000109 return SaveAs(**options).show()
Guido van Rossum1e8c8a21997-07-19 20:02:36 +0000110
Martin v. Löwisb24e3472002-10-13 10:28:04 +0000111def askopenfilenames(**options):
112 """Ask for multiple filenames to open
113
114 Returns a list of filenames or empty list if
115 cancel button selected
116 """
117 options["multiple"]=1
118 files=Open(**options).show()
119 return files.split()
120
121
122# FIXME: are the following perhaps a bit too convenient?
Guido van Rossum1e8c8a21997-07-19 20:02:36 +0000123
124def askopenfile(mode = "r", **options):
125 "Ask for a filename to open, and returned the opened file"
126
Martin v. Löwis25ee87c2001-11-07 22:38:08 +0000127 filename = Open(**options).show()
Guido van Rossum1e8c8a21997-07-19 20:02:36 +0000128 if filename:
129 return open(filename, mode)
130 return None
131
Martin v. Löwisb24e3472002-10-13 10:28:04 +0000132def askopenfiles(mode = "r", **options):
133 """Ask for multiple filenames and return the open file
134 objects
135
136 returns a list of open file objects or an empty list if
137 cancel selected
138 """
139
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
147
148
Guido van Rossum1e8c8a21997-07-19 20:02:36 +0000149def asksaveasfile(mode = "w", **options):
150 "Ask for a filename to save as, and returned the opened file"
151
Martin v. Löwis25ee87c2001-11-07 22:38:08 +0000152 filename = SaveAs(**options).show()
Guido van Rossum1e8c8a21997-07-19 20:02:36 +0000153 if filename:
154 return open(filename, mode)
155 return None
156
Martin v. Löwis25ee87c2001-11-07 22:38:08 +0000157def askdirectory (**options):
158 "Ask for a directory, and return the file name"
159 return Directory(**options).show()
Guido van Rossum1e8c8a21997-07-19 20:02:36 +0000160
161# --------------------------------------------------------------------
162# test stuff
163
164if __name__ == "__main__":
Martin v. Löwis85f98142001-12-30 14:43:56 +0000165 # 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.
Guido van Rossum1e8c8a21997-07-19 20:02:36 +0000168
Martin v. Löwis85f98142001-12-30 14:43:56 +0000169 # Start off with UTF-8
170 enc = "utf-8"
Martin v. Löwis8509ebc2002-04-08 14:51:31 +0000171 import sys
Martin v. Löwis85f98142001-12-30 14:43:56 +0000172
173 # See whether CODESET is defined
174 try:
175 import locale
Martin v. Löwis8509ebc2002-04-08 14:51:31 +0000176 locale.setlocale(locale.LC_ALL,'')
Martin v. Löwis85f98142001-12-30 14:43:56 +0000177 enc = locale.nl_langinfo(locale.CODESET)
178 except (ImportError, AttributeError):
179 pass
180
Martin v. Löwis8509ebc2002-04-08 14:51:31 +0000181 # dialog for openening files
182
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]
190
191 print "open", openfilename.encode(enc)
192
193 # dialog for saving files
194
195 saveasfilename=asksaveasfilename()
196 print "saveas", saveasfilename.encode(enc)
Martin v. Löwis85f98142001-12-30 14:43:56 +0000197