annotate Lib/lib-tk/tkFileDialog.py @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents 5cec436d64a2
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
47182
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
1 #
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
2 # Instant Python
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
3 # $Id: tkFileDialog.py 36560 2004-07-18 06:16:08Z tim_one $
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
4 #
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
5 # tk common file dialogues
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
6 #
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
7 # this module provides interfaces to the native file dialogues
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
8 # available in Tk 4.2 and newer, and the directory dialogue available
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
9 # in Tk 8.3 and newer.
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
10 #
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
11 # written by Fredrik Lundh, May 1997.
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
12 #
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
13
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
14 #
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
15 # options (all have default values):
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
16 #
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
17 # - defaultextension: added to filename if not explicitly given
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
18 #
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
19 # - filetypes: sequence of (label, pattern) tuples. the same pattern
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
20 # may occur with several patterns. use "*" as pattern to indicate
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
21 # all files.
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
22 #
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
23 # - initialdir: initial directory. preserved by dialog instance.
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
24 #
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
25 # - initialfile: initial file (ignored by the open dialog). preserved
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
26 # by dialog instance.
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
27 #
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
28 # - parent: which window to place the dialog on top of
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
29 #
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
30 # - title: dialog title
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
31 #
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
32 # - multiple: if true user may select more than one file
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
33 #
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
34 # options for the directory chooser:
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
35 #
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
36 # - initialdir, parent, title: see above
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
37 #
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
38 # - mustexist: if true, user must pick an existing directory
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
39 #
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
40 #
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
41
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
42
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
43 from tkCommonDialog import Dialog
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
44
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
45 class _Dialog(Dialog):
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
46
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
47 def _fixoptions(self):
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
48 try:
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
49 # make sure "filetypes" is a tuple
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
50 self.options["filetypes"] = tuple(self.options["filetypes"])
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
51 except KeyError:
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
52 pass
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
53
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
54 def _fixresult(self, widget, result):
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
55 if result:
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
56 # keep directory and filename until next time
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
57 import os
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
58 # convert Tcl path objects to strings
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
59 try:
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
60 result = result.string
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
61 except AttributeError:
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
62 # it already is a string
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
63 pass
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
64 path, file = os.path.split(result)
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
65 self.options["initialdir"] = path
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
66 self.options["initialfile"] = file
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
67 self.filename = result # compatibility
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
68 return result
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
69
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
70
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
71 #
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
72 # file dialogs
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
73
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
74 class Open(_Dialog):
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
75 "Ask for a filename to open"
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
76
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
77 command = "tk_getOpenFile"
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
78
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
79 def _fixresult(self, widget, result):
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
80 if isinstance(result, tuple):
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
81 # multiple results:
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
82 result = tuple([getattr(r, "string", r) for r in result])
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
83 if result:
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
84 import os
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
85 path, file = os.path.split(result[0])
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
86 self.options["initialdir"] = path
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
87 # don't set initialfile or filename, as we have multiple of these
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
88 return result
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
89 if not widget.tk.wantobjects() and "multiple" in self.options:
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
90 # Need to split result explicitly
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
91 return self._fixresult(widget, widget.tk.splitlist(result))
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
92 return _Dialog._fixresult(self, widget, result)
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
93
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
94 class SaveAs(_Dialog):
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
95 "Ask for a filename to save as"
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
96
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
97 command = "tk_getSaveFile"
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
98
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
99
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
100 # the directory dialog has its own _fix routines.
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
101 class Directory(Dialog):
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
102 "Ask for a directory"
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
103
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
104 command = "tk_chooseDirectory"
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
105
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
106 def _fixresult(self, widget, result):
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
107 if result:
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
108 # convert Tcl path objects to strings
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
109 try:
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
110 result = result.string
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
111 except AttributeError:
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
112 # it already is a string
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
113 pass
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
114 # keep directory until next time
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
115 self.options["initialdir"] = result
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
116 self.directory = result # compatibility
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
117 return result
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
118
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
119 #
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
120 # convenience stuff
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
121
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
122 def askopenfilename(**options):
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
123 "Ask for a filename to open"
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
124
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
125 return Open(**options).show()
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
126
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
127 def asksaveasfilename(**options):
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
128 "Ask for a filename to save as"
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
129
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
130 return SaveAs(**options).show()
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
131
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
132 def askopenfilenames(**options):
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
133 """Ask for multiple filenames to open
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
134
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
135 Returns a list of filenames or empty list if
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
136 cancel button selected
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
137 """
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
138 options["multiple"]=1
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
139 return Open(**options).show()
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
140
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
141 # FIXME: are the following perhaps a bit too convenient?
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
142
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
143 def askopenfile(mode = "r", **options):
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
144 "Ask for a filename to open, and returned the opened file"
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
145
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
146 filename = Open(**options).show()
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
147 if filename:
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
148 return open(filename, mode)
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
149 return None
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
150
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
151 def askopenfiles(mode = "r", **options):
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
152 """Ask for multiple filenames and return the open file
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
153 objects
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
154
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
155 returns a list of open file objects or an empty list if
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
156 cancel selected
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
157 """
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
158
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
159 files = askopenfilenames(**options)
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
160 if files:
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
161 ofiles=[]
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
162 for filename in files:
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
163 ofiles.append(open(filename, mode))
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
164 files=ofiles
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
165 return files
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
166
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
167
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
168 def asksaveasfile(mode = "w", **options):
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
169 "Ask for a filename to save as, and returned the opened file"
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
170
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
171 filename = SaveAs(**options).show()
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
172 if filename:
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
173 return open(filename, mode)
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
174 return None
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
175
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
176 def askdirectory (**options):
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
177 "Ask for a directory, and return the file name"
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
178 return Directory(**options).show()
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
179
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
180 # --------------------------------------------------------------------
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
181 # test stuff
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
182
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
183 if __name__ == "__main__":
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
184 # Since the file name may contain non-ASCII characters, we need
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
185 # to find an encoding that likely supports the file name, and
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
186 # displays correctly on the terminal.
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
187
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
188 # Start off with UTF-8
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
189 enc = "utf-8"
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
190 import sys
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
191
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
192 # See whether CODESET is defined
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
193 try:
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
194 import locale
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
195 locale.setlocale(locale.LC_ALL,'')
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
196 enc = locale.nl_langinfo(locale.CODESET)
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
197 except (ImportError, AttributeError):
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
198 pass
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
199
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
200 # dialog for openening files
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
201
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
202 openfilename=askopenfilename(filetypes=[("all files", "*")])
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
203 try:
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
204 fp=open(openfilename,"r")
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
205 fp.close()
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
206 except:
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
207 print "Could not open File: "
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
208 print sys.exc_info()[1]
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
209
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
210 print "open", openfilename.encode(enc)
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
211
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
212 # dialog for saving files
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
213
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
214 saveasfilename=asksaveasfilename()
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
215 print "saveas", saveasfilename.encode(enc)