annotate Lib/lib-tk/tkFileDialog.py @ 32527:0da660e05e69 legacy-trunk

"#if WITH_THREAD" is incorrect; must be #ifdef instead; WITH_THREAD isn't always set to an integer value when it's defined.
author Tim Peters <tim.peters@gmail.com>
date Wed, 07 Jul 2004 20:42:07 +0000
parents 251717566e91
children c3a096bfbd6b
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:
31056
251717566e91 Convert path objects to strings in askdirectory. Fixes #852314.
Martin v. Löwis <martin@v.loewis.de>
parents: 29478
diff changeset
108 # convert Tcl path objects to strings
251717566e91 Convert path objects to strings in askdirectory. Fixes #852314.
Martin v. Löwis <martin@v.loewis.de>
parents: 29478
diff changeset
109 try:
251717566e91 Convert path objects to strings in askdirectory. Fixes #852314.
Martin v. Löwis <martin@v.loewis.de>
parents: 29478
diff changeset
110 result = result.string
251717566e91 Convert path objects to strings in askdirectory. Fixes #852314.
Martin v. Löwis <martin@v.loewis.de>
parents: 29478
diff changeset
111 except AttributeError:
251717566e91 Convert path objects to strings in askdirectory. Fixes #852314.
Martin v. Löwis <martin@v.loewis.de>
parents: 29478
diff changeset
112 # it already is a string
251717566e91 Convert path objects to strings in askdirectory. Fixes #852314.
Martin v. Löwis <martin@v.loewis.de>
parents: 29478
diff changeset
113 pass
20917
20014716556b Merge directory chooser into tkFileDialog.
Martin v. Löwis <martin@v.loewis.de>
parents: 20914
diff changeset
114 # keep directory until next time
20014716556b Merge directory chooser into tkFileDialog.
Martin v. Löwis <martin@v.loewis.de>
parents: 20914
diff changeset
115 self.options["initialdir"] = result
20014716556b Merge directory chooser into tkFileDialog.
Martin v. Löwis <martin@v.loewis.de>
parents: 20914
diff changeset
116 self.directory = result # compatibility
20014716556b Merge directory chooser into tkFileDialog.
Martin v. Löwis <martin@v.loewis.de>
parents: 20914
diff changeset
117 return result
20014716556b Merge directory chooser into tkFileDialog.
Martin v. Löwis <martin@v.loewis.de>
parents: 20914
diff changeset
118
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 #
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 # convenience stuff
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
121
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 def askopenfilename(**options):
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 "Ask for a filename to open"
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
124
20914
93253c65d863 Patch #478654: Expose tk_chooseDirectory.
Martin v. Löwis <martin@v.loewis.de>
parents: 7731
diff changeset
125 return Open(**options).show()
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
126
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 def asksaveasfilename(**options):
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 "Ask for a filename to save as"
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
129
20914
93253c65d863 Patch #478654: Expose tk_chooseDirectory.
Martin v. Löwis <martin@v.loewis.de>
parents: 7731
diff changeset
130 return SaveAs(**options).show()
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
131
25755
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
132 def askopenfilenames(**options):
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
133 """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
134
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
135 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
136 cancel button selected
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
137 """
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
138 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
139 return Open(**options).show()
25755
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
140
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
141 # 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
142
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 def askopenfile(mode = "r", **options):
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 "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
145
20914
93253c65d863 Patch #478654: Expose tk_chooseDirectory.
Martin v. Löwis <martin@v.loewis.de>
parents: 7731
diff changeset
146 filename = Open(**options).show()
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
147 if filename:
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 return open(filename, mode)
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 return None
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
150
25755
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
151 def askopenfiles(mode = "r", **options):
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
152 """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
153 objects
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
154
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
155 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
156 cancel selected
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
157 """
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
158
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
159 files = askopenfilenames(**options)
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
160 if files:
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
161 ofiles=[]
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
162 for filename in files:
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
163 ofiles.append(open(filename, mode))
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
164 files=ofiles
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
165 return files
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
166
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
167
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 def asksaveasfile(mode = "w", **options):
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
169 "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
170
20914
93253c65d863 Patch #478654: Expose tk_chooseDirectory.
Martin v. Löwis <martin@v.loewis.de>
parents: 7731
diff changeset
171 filename = SaveAs(**options).show()
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
172 if filename:
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 return open(filename, mode)
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 return None
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
175
20914
93253c65d863 Patch #478654: Expose tk_chooseDirectory.
Martin v. Löwis <martin@v.loewis.de>
parents: 7731
diff changeset
176 def askdirectory (**options):
93253c65d863 Patch #478654: Expose tk_chooseDirectory.
Martin v. Löwis <martin@v.loewis.de>
parents: 7731
diff changeset
177 "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
178 return Directory(**options).show()
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
179
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
180 # --------------------------------------------------------------------
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
181 # test stuff
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
182
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
183 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
184 # 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
185 # 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
186 # displays correctly on the terminal.
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
187
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
188 # 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
189 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
190 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
191
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 # 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
193 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
194 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
195 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
196 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
197 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
198 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
199
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
200 # 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
201
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
202 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
203 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
204 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
205 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
206 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
207 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
208 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
209
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 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
211
289ea1d5c5ac Patch #539392: Invoke setlocale, try opening the file in demo mode.
Martin v. Löwis <martin@v.loewis.de>
parents: 21618
diff changeset
212 # 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
213
289ea1d5c5ac Patch #539392: Invoke setlocale, try opening the file in demo mode.
Martin v. Löwis <martin@v.loewis.de>
parents: 21618
diff changeset
214 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
215 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
216