Guido van Rossum | 1e8c8a2 | 1997-07-19 20:02:36 +0000 | [diff] [blame] | 1 | # |
| 2 | # Instant Python |
| 3 | # $Id$ |
| 4 | # |
| 5 | # base class for tk common dialogues |
| 6 | # |
| 7 | # this module provides a base class for accessing the common |
| 8 | # dialogues available in Tk 4.2 and newer. use tkFileDialog, |
| 9 | # tkColorChooser, and tkMessageBox to access the individual |
| 10 | # dialogs. |
| 11 | # |
| 12 | # written by Fredrik Lundh, May 1997 |
| 13 | # |
| 14 | |
| 15 | from Tkinter import * |
| 16 | import os |
| 17 | |
| 18 | class Dialog: |
| 19 | |
| 20 | command = None |
| 21 | |
| 22 | def __init__(self, master=None, **options): |
| 23 | |
| 24 | # FIXME: should this be placed on the module level instead? |
| 25 | if TkVersion < 4.2: |
| 26 | raise TclError, "this module requires Tk 4.2 or newer" |
| 27 | |
Guido van Rossum | c457048 | 1998-03-20 20:45:49 +0000 | [diff] [blame] | 28 | self.master = master |
Guido van Rossum | 1e8c8a2 | 1997-07-19 20:02:36 +0000 | [diff] [blame] | 29 | self.options = options |
| 30 | |
| 31 | def _fixoptions(self): |
| 32 | pass # hook |
| 33 | |
| 34 | def _fixresult(self, widget, result): |
| 35 | return result # hook |
| 36 | |
| 37 | def show(self, **options): |
| 38 | |
| 39 | # update instance options |
Guido van Rossum | c457048 | 1998-03-20 20:45:49 +0000 | [diff] [blame] | 40 | for k, v in options.items(): |
| 41 | self.options[k] = v |
Guido van Rossum | 1e8c8a2 | 1997-07-19 20:02:36 +0000 | [diff] [blame] | 42 | |
| 43 | self._fixoptions() |
| 44 | |
Guido van Rossum | 1530c87 | 1997-08-14 14:17:28 +0000 | [diff] [blame] | 45 | # we need a dummy widget to properly process the options |
Guido van Rossum | 1e8c8a2 | 1997-07-19 20:02:36 +0000 | [diff] [blame] | 46 | # (at least as long as we use Tkinter 1.63) |
| 47 | w = Frame(self.master) |
| 48 | |
| 49 | try: |
| 50 | |
| 51 | s = apply(w.tk.call, (self.command,) + w._options(self.options)) |
| 52 | |
| 53 | s = self._fixresult(w, s) |
| 54 | |
| 55 | finally: |
| 56 | |
| 57 | try: |
| 58 | # get rid of the widget |
| 59 | w.destroy() |
| 60 | except: |
| 61 | pass |
| 62 | |
| 63 | return s |
Guido van Rossum | 1530c87 | 1997-08-14 14:17:28 +0000 | [diff] [blame] | 64 | |