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 filedialog, 5# colorchooser, and messagebox to access the individual 6# dialogs. 7# 8# written by Fredrik Lundh, May 1997 9# 10 11__all__ = ["Dialog"] 12 13from tkinter import Frame 14 15 16class Dialog: 17 18 command = None 19 20 def __init__(self, master=None, **options): 21 if not master: 22 master = options.get('parent') 23 self.master = master 24 self.options = options 25 26 def _fixoptions(self): 27 pass # hook 28 29 def _fixresult(self, widget, result): 30 return result # hook 31 32 def show(self, **options): 33 34 # update instance options 35 for k, v in options.items(): 36 self.options[k] = v 37 38 self._fixoptions() 39 40 # we need a dummy widget to properly process the options 41 # (at least as long as we use Tkinter 1.63) 42 w = Frame(self.master) 43 44 try: 45 46 s = w.tk.call(self.command, *w._options(self.options)) 47 48 s = self._fixresult(w, s) 49 50 finally: 51 52 try: 53 # get rid of the widget 54 w.destroy() 55 except: 56 pass 57 58 return s 59