annotate Lib/lib-tk/tkFileDialog.py @ 25838:8540887bcec6 2.2

Backport of 2.183: Fix memory leak in add_subclass() found by NealN with valgrind.
author Guido van Rossum <guido@python.org>
date Fri, 18 Oct 2002 13:42:21 +0000
parents 20014716556b
children 5ff8bb25031c
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 #
20917
20014716556b Merge directory chooser into tkFileDialog.
Martin v. Löwis <martin@v.loewis.de>
parents: 20914
diff changeset
32 # options for the directory chooser:
20014716556b Merge directory chooser into tkFileDialog.
Martin v. Löwis <martin@v.loewis.de>
parents: 20914
diff changeset
33 #
20014716556b Merge directory chooser into tkFileDialog.
Martin v. Löwis <martin@v.loewis.de>
parents: 20914
diff changeset
34 # - initialdir, parent, title: see above
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 # - 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
37 #
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
38
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 from tkCommonDialog import Dialog
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
40
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 class _Dialog(Dialog):
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 def _fixoptions(self):
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 try:
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 # make sure "filetypes" is a tuple
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 self.options["filetypes"] = tuple(self.options["filetypes"])
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 except KeyError:
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 pass
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
49
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 def _fixresult(self, widget, result):
7731
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 5869
diff changeset
51 if result:
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 5869
diff changeset
52 # keep directory and filename until next time
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 import os
7731
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 5869
diff changeset
54 path, file = os.path.split(result)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 5869
diff changeset
55 self.options["initialdir"] = path
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 5869
diff changeset
56 self.options["initialfile"] = file
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 self.filename = result # compatibility
7731
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 5869
diff changeset
58 return result
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
59
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
60
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 #
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 # file dialogs
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 class Open(_Dialog):
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 "Ask for a filename to open"
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
66
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 command = "tk_getOpenFile"
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
68
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 class SaveAs(_Dialog):
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 "Ask for a filename to save as"
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 command = "tk_getSaveFile"
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
73
20917
20014716556b Merge directory chooser into tkFileDialog.
Martin v. Löwis <martin@v.loewis.de>
parents: 20914
diff changeset
74
20014716556b Merge directory chooser into tkFileDialog.
Martin v. Löwis <martin@v.loewis.de>
parents: 20914
diff changeset
75 # 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
76 class Directory(Dialog):
20914
93253c65d863 Patch #478654: Expose tk_chooseDirectory.
Martin v. Löwis <martin@v.loewis.de>
parents: 7731
diff changeset
77 "Ask for a directory"
93253c65d863 Patch #478654: Expose tk_chooseDirectory.
Martin v. Löwis <martin@v.loewis.de>
parents: 7731
diff changeset
78
93253c65d863 Patch #478654: Expose tk_chooseDirectory.
Martin v. Löwis <martin@v.loewis.de>
parents: 7731
diff changeset
79 command = "tk_chooseDirectory"
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
80
20917
20014716556b Merge directory chooser into tkFileDialog.
Martin v. Löwis <martin@v.loewis.de>
parents: 20914
diff changeset
81 def _fixresult(self, widget, result):
20014716556b Merge directory chooser into tkFileDialog.
Martin v. Löwis <martin@v.loewis.de>
parents: 20914
diff changeset
82 if result:
20014716556b Merge directory chooser into tkFileDialog.
Martin v. Löwis <martin@v.loewis.de>
parents: 20914
diff changeset
83 # keep directory until next time
20014716556b Merge directory chooser into tkFileDialog.
Martin v. Löwis <martin@v.loewis.de>
parents: 20914
diff changeset
84 self.options["initialdir"] = result
20014716556b Merge directory chooser into tkFileDialog.
Martin v. Löwis <martin@v.loewis.de>
parents: 20914
diff changeset
85 self.directory = result # compatibility
20014716556b Merge directory chooser into tkFileDialog.
Martin v. Löwis <martin@v.loewis.de>
parents: 20914
diff changeset
86 return result
20014716556b Merge directory chooser into tkFileDialog.
Martin v. Löwis <martin@v.loewis.de>
parents: 20914
diff changeset
87
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 #
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 # convenience stuff
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
90
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 def askopenfilename(**options):
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 "Ask for a filename to open"
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
93
20914
93253c65d863 Patch #478654: Expose tk_chooseDirectory.
Martin v. Löwis <martin@v.loewis.de>
parents: 7731
diff changeset
94 return Open(**options).show()
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
95
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 def asksaveasfilename(**options):
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 "Ask for a filename to save as"
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
98
20914
93253c65d863 Patch #478654: Expose tk_chooseDirectory.
Martin v. Löwis <martin@v.loewis.de>
parents: 7731
diff changeset
99 return SaveAs(**options).show()
5869
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 # FIXME: are the following two perhaps a bit too convenient?
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
102
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 def askopenfile(mode = "r", **options):
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 "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
105
20914
93253c65d863 Patch #478654: Expose tk_chooseDirectory.
Martin v. Löwis <martin@v.loewis.de>
parents: 7731
diff changeset
106 filename = Open(**options).show()
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 if filename:
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 return open(filename, mode)
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 return None
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
110
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 def asksaveasfile(mode = "w", **options):
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 "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
113
20914
93253c65d863 Patch #478654: Expose tk_chooseDirectory.
Martin v. Löwis <martin@v.loewis.de>
parents: 7731
diff changeset
114 filename = SaveAs(**options).show()
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 if filename:
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 return open(filename, mode)
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 return None
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 def askdirectory (**options):
93253c65d863 Patch #478654: Expose tk_chooseDirectory.
Martin v. Löwis <martin@v.loewis.de>
parents: 7731
diff changeset
120 "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
121 return Directory(**options).show()
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
122
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 # test stuff
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
125
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 if __name__ == "__main__":
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
127
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 print "open", askopenfilename(filetypes=[("all filez", "*")])
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 print "saveas", asksaveasfilename()