annotate Lib/lib-tk/tkFileDialog.py @ 27069:ef531482bb37 legacy-trunk

Convert Tcl path objects to strings. Fixes #661357. Provide .string attribute and __unicode for Tcl_Objs.
author Martin v. Löwis <martin@v.loewis.de>
date Sat, 04 Jan 2003 00:08:09 +0000
parents 6d35d4292dec
children 1ab9978123be
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
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 class SaveAs(_Dialog):
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 "Ask for a filename to save as"
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
81
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 command = "tk_getSaveFile"
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
83
20917
20014716556b Merge directory chooser into tkFileDialog.
Martin v. Löwis <martin@v.loewis.de>
parents: 20914
diff changeset
84
20014716556b Merge directory chooser into tkFileDialog.
Martin v. Löwis <martin@v.loewis.de>
parents: 20914
diff changeset
85 # 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
86 class Directory(Dialog):
20914
93253c65d863 Patch #478654: Expose tk_chooseDirectory.
Martin v. Löwis <martin@v.loewis.de>
parents: 7731
diff changeset
87 "Ask for a directory"
93253c65d863 Patch #478654: Expose tk_chooseDirectory.
Martin v. Löwis <martin@v.loewis.de>
parents: 7731
diff changeset
88
93253c65d863 Patch #478654: Expose tk_chooseDirectory.
Martin v. Löwis <martin@v.loewis.de>
parents: 7731
diff changeset
89 command = "tk_chooseDirectory"
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
90
20917
20014716556b Merge directory chooser into tkFileDialog.
Martin v. Löwis <martin@v.loewis.de>
parents: 20914
diff changeset
91 def _fixresult(self, widget, result):
20014716556b Merge directory chooser into tkFileDialog.
Martin v. Löwis <martin@v.loewis.de>
parents: 20914
diff changeset
92 if result:
20014716556b Merge directory chooser into tkFileDialog.
Martin v. Löwis <martin@v.loewis.de>
parents: 20914
diff changeset
93 # keep directory until next time
20014716556b Merge directory chooser into tkFileDialog.
Martin v. Löwis <martin@v.loewis.de>
parents: 20914
diff changeset
94 self.options["initialdir"] = result
20014716556b Merge directory chooser into tkFileDialog.
Martin v. Löwis <martin@v.loewis.de>
parents: 20914
diff changeset
95 self.directory = result # compatibility
20014716556b Merge directory chooser into tkFileDialog.
Martin v. Löwis <martin@v.loewis.de>
parents: 20914
diff changeset
96 return result
20014716556b Merge directory chooser into tkFileDialog.
Martin v. Löwis <martin@v.loewis.de>
parents: 20914
diff changeset
97
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 #
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 # convenience stuff
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
100
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 def askopenfilename(**options):
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 "Ask for a filename to open"
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
103
20914
93253c65d863 Patch #478654: Expose tk_chooseDirectory.
Martin v. Löwis <martin@v.loewis.de>
parents: 7731
diff changeset
104 return Open(**options).show()
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
105
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 def asksaveasfilename(**options):
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 "Ask for a filename to save as"
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
108
20914
93253c65d863 Patch #478654: Expose tk_chooseDirectory.
Martin v. Löwis <martin@v.loewis.de>
parents: 7731
diff changeset
109 return SaveAs(**options).show()
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
110
25755
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
111 def askopenfilenames(**options):
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
112 """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
113
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
114 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
115 cancel button selected
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
116 """
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
117 options["multiple"]=1
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
118 files=Open(**options).show()
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
119 return files.split()
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
120
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
121
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
122 # 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
123
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 def askopenfile(mode = "r", **options):
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 "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
126
20914
93253c65d863 Patch #478654: Expose tk_chooseDirectory.
Martin v. Löwis <martin@v.loewis.de>
parents: 7731
diff changeset
127 filename = Open(**options).show()
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 if filename:
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 return open(filename, mode)
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 return None
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 askopenfiles(mode = "r", **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 and return the open file
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
134 objects
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
135
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
136 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
137 cancel selected
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
138 """
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
139
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
140 files = askopenfilenames(**options)
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
141 if files:
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
142 ofiles=[]
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
143 for filename in files:
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
144 ofiles.append(open(filename, mode))
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
145 files=ofiles
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
146 return files
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
147
6d35d4292dec Patch #621891: Add askopenfile{name}s.
Martin v. Löwis <martin@v.loewis.de>
parents: 22885
diff changeset
148
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 def asksaveasfile(mode = "w", **options):
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 "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
151
20914
93253c65d863 Patch #478654: Expose tk_chooseDirectory.
Martin v. Löwis <martin@v.loewis.de>
parents: 7731
diff changeset
152 filename = SaveAs(**options).show()
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 if filename:
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 return open(filename, mode)
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 return None
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
156
20914
93253c65d863 Patch #478654: Expose tk_chooseDirectory.
Martin v. Löwis <martin@v.loewis.de>
parents: 7731
diff changeset
157 def askdirectory (**options):
93253c65d863 Patch #478654: Expose tk_chooseDirectory.
Martin v. Löwis <martin@v.loewis.de>
parents: 7731
diff changeset
158 "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
159 return Directory(**options).show()
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
160
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 # --------------------------------------------------------------------
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 # test stuff
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
163
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 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
165 # 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
166 # 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
167 # displays correctly on the terminal.
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
168
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
169 # 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
170 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
171 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
172
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 # 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
174 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
175 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
176 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
177 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
178 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
179 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
180
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
181 # 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
182
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
183 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
184 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
185 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
186 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
187 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
188 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
189 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
190
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 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
192
289ea1d5c5ac Patch #539392: Invoke setlocale, try opening the file in demo mode.
Martin v. Löwis <martin@v.loewis.de>
parents: 21618
diff changeset
193 # 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
194
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 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
196 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
197