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