Mercurial > cpython-lru_cache-weakref
annotate Lib/lib-tk/tkCommonDialog.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 | 847ae40074d4 |
| children | c5d3e458749e |
| 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 # base class for tk common 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 a base class for accessing the common |
|
f2bf7bcf8b8b
New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8 # dialogues available in Tk 4.2 and newer. use tkFileDialog, |
|
f2bf7bcf8b8b
New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
9 # tkColorChooser, and tkMessageBox to access the individual |
|
f2bf7bcf8b8b
New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
10 # dialogs. |
|
f2bf7bcf8b8b
New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
11 # |
|
f2bf7bcf8b8b
New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
12 # written by Fredrik Lundh, May 1997 |
|
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 from Tkinter import * |
|
f2bf7bcf8b8b
New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
16 import os |
|
f2bf7bcf8b8b
New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
17 |
|
f2bf7bcf8b8b
New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
18 class Dialog: |
|
f2bf7bcf8b8b
New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
19 |
|
f2bf7bcf8b8b
New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
20 command = None |
|
f2bf7bcf8b8b
New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
21 |
|
f2bf7bcf8b8b
New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
22 def __init__(self, master=None, **options): |
|
f2bf7bcf8b8b
New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
23 |
|
f2bf7bcf8b8b
New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
24 # FIXME: should this be placed on the module level instead? |
|
f2bf7bcf8b8b
New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
25 if TkVersion < 4.2: |
|
f2bf7bcf8b8b
New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
26 raise TclError, "this module requires Tk 4.2 or newer" |
|
f2bf7bcf8b8b
New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
27 |
|
7731
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
6013
diff
changeset
|
28 self.master = master |
|
5869
f2bf7bcf8b8b
New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
29 self.options = options |
|
9331
75237ca56aff
When no master widget is specified, use options['parent'] if it exists.
Guido van Rossum <guido@python.org>
parents:
7731
diff
changeset
|
30 if not master and options.get('parent'): |
|
75237ca56aff
When no master widget is specified, use options['parent'] if it exists.
Guido van Rossum <guido@python.org>
parents:
7731
diff
changeset
|
31 self.master = options['parent'] |
|
5869
f2bf7bcf8b8b
New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
32 |
|
f2bf7bcf8b8b
New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
33 def _fixoptions(self): |
|
f2bf7bcf8b8b
New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
34 pass # hook |
|
f2bf7bcf8b8b
New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
35 |
|
f2bf7bcf8b8b
New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
36 def _fixresult(self, widget, result): |
|
f2bf7bcf8b8b
New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
37 return result # hook |
|
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 def show(self, **options): |
|
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 # update instance options |
|
7731
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
6013
diff
changeset
|
42 for k, v in options.items(): |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
6013
diff
changeset
|
43 self.options[k] = v |
|
5869
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 self._fixoptions() |
|
f2bf7bcf8b8b
New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
46 |
|
6013
0b5a23ea7515
Fred Lundh's latest versions.
Guido van Rossum <guido@python.org>
parents:
5869
diff
changeset
|
47 # we need a dummy widget to properly process the options |
|
5869
f2bf7bcf8b8b
New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
48 # (at least as long as we use Tkinter 1.63) |
|
f2bf7bcf8b8b
New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
49 w = Frame(self.master) |
|
f2bf7bcf8b8b
New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
50 |
|
f2bf7bcf8b8b
New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
51 try: |
|
f2bf7bcf8b8b
New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
52 |
|
f2bf7bcf8b8b
New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
53 s = apply(w.tk.call, (self.command,) + w._options(self.options)) |
|
f2bf7bcf8b8b
New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
54 |
|
f2bf7bcf8b8b
New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
55 s = self._fixresult(w, s) |
|
f2bf7bcf8b8b
New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
56 |
|
f2bf7bcf8b8b
New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
57 finally: |
|
f2bf7bcf8b8b
New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
58 |
|
f2bf7bcf8b8b
New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
59 try: |
|
f2bf7bcf8b8b
New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
60 # get rid of the widget |
|
f2bf7bcf8b8b
New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
61 w.destroy() |
|
f2bf7bcf8b8b
New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
62 except: |
|
f2bf7bcf8b8b
New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
63 pass |
|
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 return s |
