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 * |
Guido van Rossum | 1e8c8a2 | 1997-07-19 20:02:36 +0000 | [diff] [blame] | 16 | |
| 17 | class Dialog: |
| 18 | |
| 19 | command = None |
| 20 | |
| 21 | def __init__(self, master=None, **options): |
| 22 | |
| 23 | # FIXME: should this be placed on the module level instead? |
| 24 | if TkVersion < 4.2: |
| 25 | raise TclError, "this module requires Tk 4.2 or newer" |
| 26 | |
Guido van Rossum | c457048 | 1998-03-20 20:45:49 +0000 | [diff] [blame] | 27 | self.master = master |
Guido van Rossum | 1e8c8a2 | 1997-07-19 20:02:36 +0000 | [diff] [blame] | 28 | self.options = options |
Guido van Rossum | 3179b36 | 1998-10-12 20:40:47 +0000 | [diff] [blame] | 29 | if not master and options.get('parent'): |
| 30 | self.master = options['parent'] |
Guido van Rossum | 1e8c8a2 | 1997-07-19 20:02:36 +0000 | [diff] [blame] | 31 | |
| 32 | def _fixoptions(self): |
| 33 | pass # hook |
| 34 | |
| 35 | def _fixresult(self, widget, result): |
| 36 | return result # hook |
| 37 | |
| 38 | def show(self, **options): |
| 39 | |
| 40 | # update instance options |
Guido van Rossum | c457048 | 1998-03-20 20:45:49 +0000 | [diff] [blame] | 41 | for k, v in options.items(): |
| 42 | self.options[k] = v |
Guido van Rossum | 1e8c8a2 | 1997-07-19 20:02:36 +0000 | [diff] [blame] | 43 | |
| 44 | self._fixoptions() |
| 45 | |
Guido van Rossum | 1530c87 | 1997-08-14 14:17:28 +0000 | [diff] [blame] | 46 | # we need a dummy widget to properly process the options |
Guido van Rossum | 1e8c8a2 | 1997-07-19 20:02:36 +0000 | [diff] [blame] | 47 | # (at least as long as we use Tkinter 1.63) |
| 48 | w = Frame(self.master) |
| 49 | |
| 50 | try: |
| 51 | |
| 52 | s = apply(w.tk.call, (self.command,) + w._options(self.options)) |
| 53 | |
| 54 | s = self._fixresult(w, s) |
| 55 | |
| 56 | finally: |
| 57 | |
| 58 | try: |
| 59 | # get rid of the widget |
| 60 | w.destroy() |
| 61 | except: |
| 62 | pass |
| 63 | |
| 64 | return s |