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