blob: 93cb27775802813aa4ef4de2168085320211f56e [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öwisbc0ad2d2001-11-08 17:51:33 +000032# options for the directory chooser:
33#
34# - initialdir, parent, title: see above
35#
36# - mustexist: if true, user must pick an existing directory
37#
Guido van Rossum1e8c8a21997-07-19 20:02:36 +000038
39from tkCommonDialog import Dialog
40
41class _Dialog(Dialog):
42
43 def _fixoptions(self):
44 try:
45 # make sure "filetypes" is a tuple
46 self.options["filetypes"] = tuple(self.options["filetypes"])
47 except KeyError:
48 pass
49
50 def _fixresult(self, widget, result):
Guido van Rossumc4570481998-03-20 20:45:49 +000051 if result:
52 # keep directory and filename until next time
Guido van Rossum1e8c8a21997-07-19 20:02:36 +000053 import os
Guido van Rossumc4570481998-03-20 20:45:49 +000054 path, file = os.path.split(result)
55 self.options["initialdir"] = path
56 self.options["initialfile"] = file
Guido van Rossum1e8c8a21997-07-19 20:02:36 +000057 self.filename = result # compatibility
Guido van Rossumc4570481998-03-20 20:45:49 +000058 return result
Guido van Rossum1e8c8a21997-07-19 20:02:36 +000059
60
61#
62# file dialogs
63
64class Open(_Dialog):
65 "Ask for a filename to open"
66
67 command = "tk_getOpenFile"
68
69class SaveAs(_Dialog):
70 "Ask for a filename to save as"
71
72 command = "tk_getSaveFile"
73
Martin v. Löwisbc0ad2d2001-11-08 17:51:33 +000074
75# the directory dialog has its own _fix routines.
76class Directory(Dialog):
Martin v. Löwis25ee87c2001-11-07 22:38:08 +000077 "Ask for a directory"
78
79 command = "tk_chooseDirectory"
Guido van Rossum1e8c8a21997-07-19 20:02:36 +000080
Martin v. Löwisbc0ad2d2001-11-08 17:51:33 +000081 def _fixresult(self, widget, result):
82 if result:
83 # keep directory until next time
84 self.options["initialdir"] = result
85 self.directory = result # compatibility
86 return result
87
Guido van Rossum1e8c8a21997-07-19 20:02:36 +000088#
89# convenience stuff
90
91def askopenfilename(**options):
92 "Ask for a filename to open"
93
Martin v. Löwis25ee87c2001-11-07 22:38:08 +000094 return Open(**options).show()
Guido van Rossum1e8c8a21997-07-19 20:02:36 +000095
96def asksaveasfilename(**options):
97 "Ask for a filename to save as"
98
Martin v. Löwis25ee87c2001-11-07 22:38:08 +000099 return SaveAs(**options).show()
Guido van Rossum1e8c8a21997-07-19 20:02:36 +0000100
101# FIXME: are the following two perhaps a bit too convenient?
102
103def askopenfile(mode = "r", **options):
104 "Ask for a filename to open, and returned the opened file"
105
Martin v. Löwis25ee87c2001-11-07 22:38:08 +0000106 filename = Open(**options).show()
Guido van Rossum1e8c8a21997-07-19 20:02:36 +0000107 if filename:
108 return open(filename, mode)
109 return None
110
111def asksaveasfile(mode = "w", **options):
112 "Ask for a filename to save as, and returned the opened file"
113
Martin v. Löwis25ee87c2001-11-07 22:38:08 +0000114 filename = SaveAs(**options).show()
Guido van Rossum1e8c8a21997-07-19 20:02:36 +0000115 if filename:
116 return open(filename, mode)
117 return None
118
Martin v. Löwis25ee87c2001-11-07 22:38:08 +0000119def askdirectory (**options):
120 "Ask for a directory, and return the file name"
121 return Directory(**options).show()
Guido van Rossum1e8c8a21997-07-19 20:02:36 +0000122
123# --------------------------------------------------------------------
124# test stuff
125
126if __name__ == "__main__":
Martin v. Löwis85f98142001-12-30 14:43:56 +0000127 # Since the file name may contain non-ASCII characters, we need
128 # to find an encoding that likely supports the file name, and
129 # displays correctly on the terminal.
Guido van Rossum1e8c8a21997-07-19 20:02:36 +0000130
Martin v. Löwis85f98142001-12-30 14:43:56 +0000131 # Start off with UTF-8
132 enc = "utf-8"
Martin v. Löwis8509ebc2002-04-08 14:51:31 +0000133 import sys
Martin v. Löwis85f98142001-12-30 14:43:56 +0000134
135 # See whether CODESET is defined
136 try:
137 import locale
Martin v. Löwis8509ebc2002-04-08 14:51:31 +0000138 locale.setlocale(locale.LC_ALL,'')
Martin v. Löwis85f98142001-12-30 14:43:56 +0000139 enc = locale.nl_langinfo(locale.CODESET)
140 except (ImportError, AttributeError):
141 pass
142
Martin v. Löwis8509ebc2002-04-08 14:51:31 +0000143 # dialog for openening files
144
145 openfilename=askopenfilename(filetypes=[("all files", "*")])
146 try:
147 fp=open(openfilename,"r")
148 fp.close()
149 except:
150 print "Could not open File: "
151 print sys.exc_info()[1]
152
153 print "open", openfilename.encode(enc)
154
155 # dialog for saving files
156
157 saveasfilename=asksaveasfilename()
158 print "saveas", saveasfilename.encode(enc)
Martin v. Löwis85f98142001-12-30 14:43:56 +0000159