annotate Lib/lib-tk/tkFileDialog.py @ 29478:deb9715bc8ae legacy-trunk

Add a missing 'self,' to a super call in recently checked-in code. This was reported in the IDLEFORK bug tracker as #754971.
author Guido van Rossum <guido@python.org>
date Sun, 15 Jun 2003 19:08:57 +0000
parents 1ab9978123be
children 38338c970f7d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 #
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 # Instant Python
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 # $Id$
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 #
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 # tk common file dialogues
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 #
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 # this module provides interfaces to the native file dialogues
20917
20014716556b Merge directory chooser into tkFileDialog.
Martin v. Löwis <martin@v.loewis.de>
parents: 20914
diff changeset
8 # available in Tk 4.2 and newer, and the directory dialogue available
20014716556b Merge directory chooser into tkFileDialog.
Martin v. Löwis <martin@v.loewis.de>
parents: 20914
diff changeset
9 # in Tk 8.3 and newer.
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 #
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 # written by Fredrik Lundh, May 1997.
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 #
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
13
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 #
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 # options (all have default values):
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 #
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 # - defaultextension: added to filename if not explicitly given
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 #
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 # - filetypes: sequence of (label, pattern) tuples. the same pattern
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 # may occur with several patterns. use "*" as pattern to indicate
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 # all files.
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 #
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 # - initialdir: initial directory. preserved by dialog instance.
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 #
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 # - initialfile: initial file (ignored by the open dialog). preserved
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 # by dialog instance.
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 #
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 # - parent: which window to place the dialog on top of
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 #
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 # - title: dialog title
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 #
25755
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
32 # - multiple: if true user may select more than one file
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
33 #
20917
20014716556b Merge directory chooser into tkFileDialog.
Martin v. Löwis <martin@v.loewis.de>
parents: 20914
diff changeset
34 # options for the directory chooser:
20014716556b Merge directory chooser into tkFileDialog.
Martin v. Löwis <martin@v.loewis.de>
parents: 20914
diff changeset
35 #
20014716556b Merge directory chooser into tkFileDialog.
Martin v. Löwis <martin@v.loewis.de>
parents: 20914
diff changeset
36 # - initialdir, parent, title: see above
20014716556b Merge directory chooser into tkFileDialog.
Martin v. Löwis <martin@v.loewis.de>
parents: 20914
diff changeset
37 #
20014716556b Merge directory chooser into tkFileDialog.
Martin v. Löwis <martin@v.loewis.de>
parents: 20914
diff changeset
38 # - mustexist: if true, user must pick an existing directory
20014716556b Merge directory chooser into tkFileDialog.
Martin v. Löwis <martin@v.loewis.de>
parents: 20914
diff changeset
39 #
25755
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
40 #
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
41
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
42
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 from tkCommonDialog import Dialog
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
44
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 class _Dialog(Dialog):
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
46
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 def _fixoptions(self):
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 try:
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 # make sure "filetypes" is a tuple
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 self.options["filetypes"] = tuple(self.options["filetypes"])
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 except KeyError:
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 pass
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
53
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 def _fixresult(self, widget, result):
7731
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 5869
diff changeset
55 if result:
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 5869
diff changeset
56 # keep directory and filename until next time
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 import os
27069
ef531482bb37 Convert Tcl path objects to strings. Fixes #661357.
Martin v. Löwis <martin@v.loewis.de>
parents: 25755
diff changeset
58 # convert Tcl path objects to strings
ef531482bb37 Convert Tcl path objects to strings. Fixes #661357.
Martin v. Löwis <martin@v.loewis.de>
parents: 25755
diff changeset
59 try:
ef531482bb37 Convert Tcl path objects to strings. Fixes #661357.
Martin v. Löwis <martin@v.loewis.de>
parents: 25755
diff changeset
60 result = result.string
ef531482bb37 Convert Tcl path objects to strings. Fixes #661357.
Martin v. Löwis <martin@v.loewis.de>
parents: 25755
diff changeset
61 except AttributeError:
ef531482bb37 Convert Tcl path objects to strings. Fixes #661357.
Martin v. Löwis <martin@v.loewis.de>
parents: 25755
diff changeset
62 # it already is a string
ef531482bb37 Convert Tcl path objects to strings. Fixes #661357.
Martin v. Löwis <martin@v.loewis.de>
parents: 25755
diff changeset
63 pass
7731
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 5869
diff changeset
64 path, file = os.path.split(result)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 5869
diff changeset
65 self.options["initialdir"] = path
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 5869
diff changeset
66 self.options["initialfile"] = file
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 self.filename = result # compatibility
7731
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 5869
diff changeset
68 return result
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
69
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
70
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 #
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 # file dialogs
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
73
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 class Open(_Dialog):
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 "Ask for a filename to open"
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
76
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 command = "tk_getOpenFile"
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
78
29472
1ab9978123be Properly deal with tuples in Open._fixresult. Fixes bug reported in
Martin v. Löwis <martin@v.loewis.de>
parents: 27069
diff changeset
79 def _fixresult(self, widget, result):
1ab9978123be Properly deal with tuples in Open._fixresult. Fixes bug reported in
Martin v. Löwis <martin@v.loewis.de>
parents: 27069
diff changeset
80 if isinstance(result, tuple):
1ab9978123be Properly deal with tuples in Open._fixresult. Fixes bug reported in
Martin v. Löwis <martin@v.loewis.de>
parents: 27069
diff changeset
81 # multiple results:
1ab9978123be Properly deal with tuples in Open._fixresult. Fixes bug reported in
Martin v. Löwis <martin@v.loewis.de>
parents: 27069
diff changeset
82 result = tuple([getattr(r, "string", r) for r in result])
1ab9978123be Properly deal with tuples in Open._fixresult. Fixes bug reported in
Martin v. Löwis <martin@v.loewis.de>
parents: 27069
diff changeset
83 if result:
1ab9978123be Properly deal with tuples in Open._fixresult. Fixes bug reported in
Martin v. Löwis <martin@v.loewis.de>
parents: 27069
diff changeset
84 import os
1ab9978123be Properly deal with tuples in Open._fixresult. Fixes bug reported in
Martin v. Löwis <martin@v.loewis.de>
parents: 27069
diff changeset
85 path, file = os.path.split(result[0])
1ab9978123be Properly deal with tuples in Open._fixresult. Fixes bug reported in
Martin v. Löwis <martin@v.loewis.de>
parents: 27069
diff changeset
86 self.options["initialdir"] = path
1ab9978123be Properly deal with tuples in Open._fixresult. Fixes bug reported in
Martin v. Löwis <martin@v.loewis.de>
parents: 27069
diff changeset
87 # don't set initialfile or filename, as we have multiple of these
1ab9978123be Properly deal with tuples in Open._fixresult. Fixes bug reported in
Martin v. Löwis <martin@v.loewis.de>
parents: 27069
diff changeset
88 return result
1ab9978123be Properly deal with tuples in Open._fixresult. Fixes bug reported in
Martin v. Löwis <martin@v.loewis.de>
parents: 27069
diff changeset
89 if not widget.tk.wantobjects() and "multiple" in self.options:
1ab9978123be Properly deal with tuples in Open._fixresult. Fixes bug reported in
Martin v. Löwis <martin@v.loewis.de>
parents: 27069
diff changeset
90 # Need to split result explicitly
1ab9978123be Properly deal with tuples in Open._fixresult. Fixes bug reported in
Martin v. Löwis <martin@v.loewis.de>
parents: 27069
diff changeset
91 return self._fixresult(widget, widget.tk.splitlist(result))
29478
deb9715bc8ae Add a missing 'self,' to a super call in recently checked-in code.
Guido van Rossum <guido@python.org>
parents: 29472
diff changeset
92 return _Dialog._fixresult(self, widget, result)
29472
1ab9978123be Properly deal with tuples in Open._fixresult. Fixes bug reported in
Martin v. Löwis <martin@v.loewis.de>
parents: 27069
diff changeset
93
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 class SaveAs(_Dialog):
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 "Ask for a filename to save as"
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
96
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 command = "tk_getSaveFile"
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
98
20917
20014716556b Merge directory chooser into tkFileDialog.
Martin v. Löwis <martin@v.loewis.de>
parents: 20914
diff changeset
99
20014716556b Merge directory chooser into tkFileDialog.
Martin v. Löwis <martin@v.loewis.de>
parents: 20914
diff changeset
100 # the directory dialog has its own _fix routines.
20014716556b Merge directory chooser into tkFileDialog.
Martin v. Löwis <martin@v.loewis.de>
parents: 20914
diff changeset
101 class Directory(Dialog):
20914
93253c65d863 Patch #478654: Expose tk_chooseDirectory.
Martin v. Löwis <martin@v.loewis.de>
parents: 7731
diff changeset
102 "Ask for a directory"
93253c65d863 Patch #478654: Expose tk_chooseDirectory.
Martin v. Löwis <martin@v.loewis.de>
parents: 7731
diff changeset
103
93253c65d863 Patch #478654: Expose tk_chooseDirectory.
Martin v. Löwis <martin@v.loewis.de>
parents: 7731
diff changeset
104 command = "tk_chooseDirectory"
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
105
20917
20014716556b Merge directory chooser into tkFileDialog.
Martin v. Löwis <martin@v.loewis.de>
parents: 20914
diff changeset
106 def _fixresult(self, widget, result):
20014716556b Merge directory chooser into tkFileDialog.
Martin v. Löwis <martin@v.loewis.de>
parents: 20914
diff changeset
107 if result:
20014716556b Merge directory chooser into tkFileDialog.
Martin v. Löwis <martin@v.loewis.de>
parents: 20914
diff changeset
108 # keep directory until next time
20014716556b Merge directory chooser into tkFileDialog.
Martin v. Löwis <martin@v.loewis.de>
parents: 20914
diff changeset
109 self.options["initialdir"] = result
20014716556b Merge directory chooser into tkFileDialog.
Martin v. Löwis <martin@v.loewis.de>
parents: 20914
diff changeset
110 self.directory = result # compatibility
20014716556b Merge directory chooser into tkFileDialog.
Martin v. Löwis <martin@v.loewis.de>
parents: 20914
diff changeset
111 return result
20014716556b Merge directory chooser into tkFileDialog.
Martin v. Löwis <martin@v.loewis.de>
parents: 20914
diff changeset
112
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 #
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 # convenience stuff
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
115
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 def askopenfilename(**options):
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 "Ask for a filename to open"
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
118
20914
93253c65d863 Patch #478654: Expose tk_chooseDirectory.
Martin v. Löwis <martin@v.loewis.de>
parents: 7731
diff changeset
119 return Open(**options).show()
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
120
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 def asksaveasfilename(**options):
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 "Ask for a filename to save as"
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
123
20914
93253c65d863 Patch #478654: Expose tk_chooseDirectory.
Martin v. Löwis <martin@v.loewis.de>
parents: 7731
diff changeset
124 return SaveAs(**options).show()
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
125
25755
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
126 def askopenfilenames(**options):
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
127 """Ask for multiple filenames to open
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
128
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
129 Returns a list of filenames or empty list if
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
130 cancel button selected
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
131 """
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
132 options["multiple"]=1
29472
1ab9978123be Properly deal with tuples in Open._fixresult. Fixes bug reported in
Martin v. Löwis <martin@v.loewis.de>
parents: 27069
diff changeset
133 return Open(**options).show()
25755
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
134
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
135 # FIXME: are the following perhaps a bit too convenient?
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
136
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 def askopenfile(mode = "r", **options):
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 "Ask for a filename to open, and returned the opened file"
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
139
20914
93253c65d863 Patch #478654: Expose tk_chooseDirectory.
Martin v. Löwis <martin@v.loewis.de>
parents: 7731
diff changeset
140 filename = Open(**options).show()
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 if filename:
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 return open(filename, mode)
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 return None
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
144
25755
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
145 def askopenfiles(mode = "r", **options):
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
146 """Ask for multiple filenames and return the open file
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
147 objects
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
148
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
149 returns a list of open file objects or an empty list if
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
150 cancel selected
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
151 """
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
152
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
153 files = askopenfilenames(**options)
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
154 if files:
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
155 ofiles=[]
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
156 for filename in files:
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
157 ofiles.append(open(filename, mode))
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
158 files=ofiles
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
159 return files
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
160
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
161
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 def asksaveasfile(mode = "w", **options):
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 "Ask for a filename to save as, and returned the opened file"
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
164
20914
93253c65d863 Patch #478654: Expose tk_chooseDirectory.
Martin v. Löwis <martin@v.loewis.de>
parents: 7731
diff changeset
165 filename = SaveAs(**options).show()
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 if filename:
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
167 return open(filename, mode)
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 return None
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
169
20914
93253c65d863 Patch #478654: Expose tk_chooseDirectory.
Martin v. Löwis <martin@v.loewis.de>
parents: 7731
diff changeset
170 def askdirectory (**options):
93253c65d863 Patch #478654: Expose tk_chooseDirectory.
Martin v. Löwis <martin@v.loewis.de>
parents: 7731
diff changeset
171 "Ask for a directory, and return the file name"
93253c65d863 Patch #478654: Expose tk_chooseDirectory.
Martin v. Löwis <martin@v.loewis.de>
parents: 7731
diff changeset
172 return Directory(**options).show()
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
173
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 # --------------------------------------------------------------------
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 # test stuff
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
176
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 if __name__ == "__main__":
21596
5ff8bb25031c Print encoded versions of the file names in test application. Fixes #496084
Martin v. Löwis <martin@v.loewis.de>
parents: 20917
diff changeset
178 # Since the file name may contain non-ASCII characters, we need
5ff8bb25031c Print encoded versions of the file names in test application. Fixes #496084
Martin v. Löwis <martin@v.loewis.de>
parents: 20917
diff changeset
179 # to find an encoding that likely supports the file name, and
5ff8bb25031c Print encoded versions of the file names in test application. Fixes #496084
Martin v. Löwis <martin@v.loewis.de>
parents: 20917
diff changeset
180 # displays correctly on the terminal.
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
181
21596
5ff8bb25031c Print encoded versions of the file names in test application. Fixes #496084
Martin v. Löwis <martin@v.loewis.de>
parents: 20917
diff changeset
182 # Start off with UTF-8
5ff8bb25031c Print encoded versions of the file names in test application. Fixes #496084
Martin v. Löwis <martin@v.loewis.de>
parents: 20917
diff changeset
183 enc = "utf-8"
22885
289ea1d5c5ac Patch #539392: Invoke setlocale, try opening the file in demo mode.
Martin v. Löwis <martin@v.loewis.de>
parents: 21618
diff changeset
184 import sys
21596
5ff8bb25031c Print encoded versions of the file names in test application. Fixes #496084
Martin v. Löwis <martin@v.loewis.de>
parents: 20917
diff changeset
185
5ff8bb25031c Print encoded versions of the file names in test application. Fixes #496084
Martin v. Löwis <martin@v.loewis.de>
parents: 20917
diff changeset
186 # See whether CODESET is defined
5ff8bb25031c Print encoded versions of the file names in test application. Fixes #496084
Martin v. Löwis <martin@v.loewis.de>
parents: 20917
diff changeset
187 try:
5ff8bb25031c Print encoded versions of the file names in test application. Fixes #496084
Martin v. Löwis <martin@v.loewis.de>
parents: 20917
diff changeset
188 import locale
22885
289ea1d5c5ac Patch #539392: Invoke setlocale, try opening the file in demo mode.
Martin v. Löwis <martin@v.loewis.de>
parents: 21618
diff changeset
189 locale.setlocale(locale.LC_ALL,'')
21596
5ff8bb25031c Print encoded versions of the file names in test application. Fixes #496084
Martin v. Löwis <martin@v.loewis.de>
parents: 20917
diff changeset
190 enc = locale.nl_langinfo(locale.CODESET)
5ff8bb25031c Print encoded versions of the file names in test application. Fixes #496084
Martin v. Löwis <martin@v.loewis.de>
parents: 20917
diff changeset
191 except (ImportError, AttributeError):
5ff8bb25031c Print encoded versions of the file names in test application. Fixes #496084
Martin v. Löwis <martin@v.loewis.de>
parents: 20917
diff changeset
192 pass
5ff8bb25031c Print encoded versions of the file names in test application. Fixes #496084
Martin v. Löwis <martin@v.loewis.de>
parents: 20917
diff changeset
193
22885
289ea1d5c5ac Patch #539392: Invoke setlocale, try opening the file in demo mode.
Martin v. Löwis <martin@v.loewis.de>
parents: 21618
diff changeset
194 # dialog for openening files
21596
5ff8bb25031c Print encoded versions of the file names in test application. Fixes #496084
Martin v. Löwis <martin@v.loewis.de>
parents: 20917
diff changeset
195
22885
289ea1d5c5ac Patch #539392: Invoke setlocale, try opening the file in demo mode.
Martin v. Löwis <martin@v.loewis.de>
parents: 21618
diff changeset
196 openfilename=askopenfilename(filetypes=[("all files", "*")])
289ea1d5c5ac Patch #539392: Invoke setlocale, try opening the file in demo mode.
Martin v. Löwis <martin@v.loewis.de>
parents: 21618
diff changeset
197 try:
289ea1d5c5ac Patch #539392: Invoke setlocale, try opening the file in demo mode.
Martin v. Löwis <martin@v.loewis.de>
parents: 21618
diff changeset
198 fp=open(openfilename,"r")
289ea1d5c5ac Patch #539392: Invoke setlocale, try opening the file in demo mode.
Martin v. Löwis <martin@v.loewis.de>
parents: 21618
diff changeset
199 fp.close()
289ea1d5c5ac Patch #539392: Invoke setlocale, try opening the file in demo mode.
Martin v. Löwis <martin@v.loewis.de>
parents: 21618
diff changeset
200 except:
289ea1d5c5ac Patch #539392: Invoke setlocale, try opening the file in demo mode.
Martin v. Löwis <martin@v.loewis.de>
parents: 21618
diff changeset
201 print "Could not open File: "
289ea1d5c5ac Patch #539392: Invoke setlocale, try opening the file in demo mode.
Martin v. Löwis <martin@v.loewis.de>
parents: 21618
diff changeset
202 print sys.exc_info()[1]
289ea1d5c5ac Patch #539392: Invoke setlocale, try opening the file in demo mode.
Martin v. Löwis <martin@v.loewis.de>
parents: 21618
diff changeset
203
289ea1d5c5ac Patch #539392: Invoke setlocale, try opening the file in demo mode.
Martin v. Löwis <martin@v.loewis.de>
parents: 21618
diff changeset
204 print "open", openfilename.encode(enc)
289ea1d5c5ac Patch #539392: Invoke setlocale, try opening the file in demo mode.
Martin v. Löwis <martin@v.loewis.de>
parents: 21618
diff changeset
205
289ea1d5c5ac Patch #539392: Invoke setlocale, try opening the file in demo mode.
Martin v. Löwis <martin@v.loewis.de>
parents: 21618
diff changeset
206 # dialog for saving files
289ea1d5c5ac Patch #539392: Invoke setlocale, try opening the file in demo mode.
Martin v. Löwis <martin@v.loewis.de>
parents: 21618
diff changeset
207
289ea1d5c5ac Patch #539392: Invoke setlocale, try opening the file in demo mode.
Martin v. Löwis <martin@v.loewis.de>
parents: 21618
diff changeset
208 saveasfilename=asksaveasfilename()
289ea1d5c5ac Patch #539392: Invoke setlocale, try opening the file in demo mode.
Martin v. Löwis <martin@v.loewis.de>
parents: 21618
diff changeset
209 print "saveas", saveasfilename.encode(enc)
289ea1d5c5ac Patch #539392: Invoke setlocale, try opening the file in demo mode.
Martin v. Löwis <martin@v.loewis.de>
parents: 21618
diff changeset
210