annotate Lib/lib-tk/tkFileDialog.py @ 25755:6d35d4292dec legacy-trunk

Patch #621891: Add askopenfile{name}s.
author Martin v. Löwis <martin@v.loewis.de>
date Sun, 13 Oct 2002 10:28:04 +0000
parents 289ea1d5c5ac
children ef531482bb37
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
7731
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 5869
diff changeset
58 path, file = os.path.split(result)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 5869
diff changeset
59 self.options["initialdir"] = path
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 5869
diff changeset
60 self.options["initialfile"] = file
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 self.filename = result # compatibility
7731
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 5869
diff changeset
62 return result
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
63
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
64
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 #
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 # file dialogs
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
67
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 class Open(_Dialog):
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 "Ask for a filename to open"
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 command = "tk_getOpenFile"
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
72
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 class SaveAs(_Dialog):
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 "Ask for a filename to save as"
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
75
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 command = "tk_getSaveFile"
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
77
20917
20014716556b Merge directory chooser into tkFileDialog.
Martin v. Löwis <martin@v.loewis.de>
parents: 20914
diff changeset
78
20014716556b Merge directory chooser into tkFileDialog.
Martin v. Löwis <martin@v.loewis.de>
parents: 20914
diff changeset
79 # 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
80 class Directory(Dialog):
20914
93253c65d863 Patch #478654: Expose tk_chooseDirectory.
Martin v. Löwis <martin@v.loewis.de>
parents: 7731
diff changeset
81 "Ask for a directory"
93253c65d863 Patch #478654: Expose tk_chooseDirectory.
Martin v. Löwis <martin@v.loewis.de>
parents: 7731
diff changeset
82
93253c65d863 Patch #478654: Expose tk_chooseDirectory.
Martin v. Löwis <martin@v.loewis.de>
parents: 7731
diff changeset
83 command = "tk_chooseDirectory"
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
84
20917
20014716556b Merge directory chooser into tkFileDialog.
Martin v. Löwis <martin@v.loewis.de>
parents: 20914
diff changeset
85 def _fixresult(self, widget, result):
20014716556b Merge directory chooser into tkFileDialog.
Martin v. Löwis <martin@v.loewis.de>
parents: 20914
diff changeset
86 if result:
20014716556b Merge directory chooser into tkFileDialog.
Martin v. Löwis <martin@v.loewis.de>
parents: 20914
diff changeset
87 # keep directory until next time
20014716556b Merge directory chooser into tkFileDialog.
Martin v. Löwis <martin@v.loewis.de>
parents: 20914
diff changeset
88 self.options["initialdir"] = result
20014716556b Merge directory chooser into tkFileDialog.
Martin v. Löwis <martin@v.loewis.de>
parents: 20914
diff changeset
89 self.directory = result # compatibility
20014716556b Merge directory chooser into tkFileDialog.
Martin v. Löwis <martin@v.loewis.de>
parents: 20914
diff changeset
90 return result
20014716556b Merge directory chooser into tkFileDialog.
Martin v. Löwis <martin@v.loewis.de>
parents: 20914
diff changeset
91
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 #
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 # convenience stuff
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
94
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 def askopenfilename(**options):
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 "Ask for a filename to open"
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
97
20914
93253c65d863 Patch #478654: Expose tk_chooseDirectory.
Martin v. Löwis <martin@v.loewis.de>
parents: 7731
diff changeset
98 return Open(**options).show()
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
99
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 def asksaveasfilename(**options):
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 "Ask for a filename to save as"
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
102
20914
93253c65d863 Patch #478654: Expose tk_chooseDirectory.
Martin v. Löwis <martin@v.loewis.de>
parents: 7731
diff changeset
103 return SaveAs(**options).show()
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
104
25755
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
105 def askopenfilenames(**options):
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
106 """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
107
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
108 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
109 cancel button selected
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
110 """
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
111 options["multiple"]=1
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
112 files=Open(**options).show()
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
113 return files.split()
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
114
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
115
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
116 # 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
117
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 def askopenfile(mode = "r", **options):
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 "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
120
20914
93253c65d863 Patch #478654: Expose tk_chooseDirectory.
Martin v. Löwis <martin@v.loewis.de>
parents: 7731
diff changeset
121 filename = Open(**options).show()
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 if filename:
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 return open(filename, mode)
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 return None
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 askopenfiles(mode = "r", **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 and return the open file
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
128 objects
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
129
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
130 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
131 cancel selected
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
132 """
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
133
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
134 files = askopenfilenames(**options)
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
135 if files:
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
136 ofiles=[]
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
137 for filename in files:
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
138 ofiles.append(open(filename, mode))
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
139 files=ofiles
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
140 return files
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
141
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
142
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 def asksaveasfile(mode = "w", **options):
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 "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
145
20914
93253c65d863 Patch #478654: Expose tk_chooseDirectory.
Martin v. Löwis <martin@v.loewis.de>
parents: 7731
diff changeset
146 filename = SaveAs(**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
20914
93253c65d863 Patch #478654: Expose tk_chooseDirectory.
Martin v. Löwis <martin@v.loewis.de>
parents: 7731
diff changeset
151 def askdirectory (**options):
93253c65d863 Patch #478654: Expose tk_chooseDirectory.
Martin v. Löwis <martin@v.loewis.de>
parents: 7731
diff changeset
152 "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
153 return Directory(**options).show()
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
154
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 # --------------------------------------------------------------------
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 # test stuff
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
157
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 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
159 # 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
160 # 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
161 # displays correctly on the terminal.
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
162
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
163 # 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
164 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
165 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
166
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
167 # 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
168 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
169 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
170 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
171 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
172 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
173 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
174
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
175 # 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
176
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
177 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
178 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
179 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
180 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
181 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
182 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
183 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
184
289ea1d5c5ac Patch #539392: Invoke setlocale, try opening the file in demo mode.
Martin v. Löwis <martin@v.loewis.de>
parents: 21618
diff changeset
185 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
186
289ea1d5c5ac Patch #539392: Invoke setlocale, try opening the file in demo mode.
Martin v. Löwis <martin@v.loewis.de>
parents: 21618
diff changeset
187 # 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
188
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 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
190 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
191