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