blob: 569254a456515301ad7a4f221623196d1a34abda [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
Guido van Rossumc4570481998-03-20 20:45:49 +000058 path, file = os.path.split(result)
59 self.options["initialdir"] = path
60 self.options["initialfile"] = file
Guido van Rossum1e8c8a21997-07-19 20:02:36 +000061 self.filename = result # compatibility
Guido van Rossumc4570481998-03-20 20:45:49 +000062 return result
Guido van Rossum1e8c8a21997-07-19 20:02:36 +000063
64
65#
66# file dialogs
67
68class Open(_Dialog):
69 "Ask for a filename to open"
70
71 command = "tk_getOpenFile"
72
73class SaveAs(_Dialog):
74 "Ask for a filename to save as"
75
76 command = "tk_getSaveFile"
77
Martin v. Löwisbc0ad2d2001-11-08 17:51:33 +000078
79# the directory dialog has its own _fix routines.
80class Directory(Dialog):
Martin v. Löwis25ee87c2001-11-07 22:38:08 +000081 "Ask for a directory"
82
83 command = "tk_chooseDirectory"
Guido van Rossum1e8c8a21997-07-19 20:02:36 +000084
Martin v. Löwisbc0ad2d2001-11-08 17:51:33 +000085 def _fixresult(self, widget, result):
86 if result:
87 # keep directory until next time
88 self.options["initialdir"] = result
89 self.directory = result # compatibility
90 return result
91
Guido van Rossum1e8c8a21997-07-19 20:02:36 +000092#
93# convenience stuff
94
95def askopenfilename(**options):
96 "Ask for a filename to open"
97
Martin v. Löwis25ee87c2001-11-07 22:38:08 +000098 return Open(**options).show()
Guido van Rossum1e8c8a21997-07-19 20:02:36 +000099
100def asksaveasfilename(**options):
101 "Ask for a filename to save as"
102
Martin v. Löwis25ee87c2001-11-07 22:38:08 +0000103 return SaveAs(**options).show()
Guido van Rossum1e8c8a21997-07-19 20:02:36 +0000104
Martin v. Löwisb24e3472002-10-13 10:28:04 +0000105def askopenfilenames(**options):
106 """Ask for multiple filenames to open
107
108 Returns a list of filenames or empty list if
109 cancel button selected
110 """
111 options["multiple"]=1
112 files=Open(**options).show()
113 return files.split()
114
115
116# FIXME: are the following perhaps a bit too convenient?
Guido van Rossum1e8c8a21997-07-19 20:02:36 +0000117
118def askopenfile(mode = "r", **options):
119 "Ask for a filename to open, and returned the opened file"
120
Martin v. Löwis25ee87c2001-11-07 22:38:08 +0000121 filename = Open(**options).show()
Guido van Rossum1e8c8a21997-07-19 20:02:36 +0000122 if filename:
123 return open(filename, mode)
124 return None
125
Martin v. Löwisb24e3472002-10-13 10:28:04 +0000126def askopenfiles(mode = "r", **options):
127 """Ask for multiple filenames and return the open file
128 objects
129
130 returns a list of open file objects or an empty list if
131 cancel selected
132 """
133
134 files = askopenfilenames(**options)
135 if files:
136 ofiles=[]
137 for filename in files:
138 ofiles.append(open(filename, mode))
139 files=ofiles
140 return files
141
142
Guido van Rossum1e8c8a21997-07-19 20:02:36 +0000143def asksaveasfile(mode = "w", **options):
144 "Ask for a filename to save as, and returned the opened file"
145
Martin v. Löwis25ee87c2001-11-07 22:38:08 +0000146 filename = SaveAs(**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öwis25ee87c2001-11-07 22:38:08 +0000151def askdirectory (**options):
152 "Ask for a directory, and return the file name"
153 return Directory(**options).show()
Guido van Rossum1e8c8a21997-07-19 20:02:36 +0000154
155# --------------------------------------------------------------------
156# test stuff
157
158if __name__ == "__main__":
Martin v. Löwis85f98142001-12-30 14:43:56 +0000159 # 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.
Guido van Rossum1e8c8a21997-07-19 20:02:36 +0000162
Martin v. Löwis85f98142001-12-30 14:43:56 +0000163 # Start off with UTF-8
164 enc = "utf-8"
Martin v. Löwis8509ebc2002-04-08 14:51:31 +0000165 import sys
Martin v. Löwis85f98142001-12-30 14:43:56 +0000166
167 # See whether CODESET is defined
168 try:
169 import locale
Martin v. Löwis8509ebc2002-04-08 14:51:31 +0000170 locale.setlocale(locale.LC_ALL,'')
Martin v. Löwis85f98142001-12-30 14:43:56 +0000171 enc = locale.nl_langinfo(locale.CODESET)
172 except (ImportError, AttributeError):
173 pass
174
Martin v. Löwis8509ebc2002-04-08 14:51:31 +0000175 # dialog for openening files
176
177 openfilename=askopenfilename(filetypes=[("all files", "*")])
178 try:
179 fp=open(openfilename,"r")
180 fp.close()
181 except:
182 print "Could not open File: "
183 print sys.exc_info()[1]
184
185 print "open", openfilename.encode(enc)
186
187 # dialog for saving files
188
189 saveasfilename=asksaveasfilename()
190 print "saveas", saveasfilename.encode(enc)
Martin v. Löwis85f98142001-12-30 14:43:56 +0000191