commondialog.py 1.38 KB
Newer Older
1 2 3
# base class for tk common dialogues
#
# this module provides a base class for accessing the common
4 5
# dialogues available in Tk 4.2 and newer.  use filedialog,
# colorchooser, and messagebox to access the individual
6 7 8 9 10
# dialogs.
#
# written by Fredrik Lundh, May 1997
#

11
from tkinter import *
12 13 14 15 16 17 18 19 20

class Dialog:

    command  = None

    def __init__(self, master=None, **options):

        # FIXME: should this be placed on the module level instead?
        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 26
        if not master and options.get('parent'):
            self.master = options['parent']
27 28 29 30 31 32 33 34 35 36

    def _fixoptions(self):
        pass # hook

    def _fixresult(self, widget, result):
        return result # hook

    def show(self, **options):

        # update instance options
37 38
        for k, v in options.items():
            self.options[k] = v
39 40 41

        self._fixoptions()

42
        # we need a dummy widget to properly process the options
43 44 45 46 47
        # (at least as long as we use Tkinter 1.63)
        w = Frame(self.master)

        try:

48
            s = w.tk.call(self.command, *w._options(self.options))
49 50 51 52 53 54 55 56 57 58 59 60

            s = self._fixresult(w, s)

        finally:

            try:
                # get rid of the widget
                w.destroy()
            except:
                pass

        return s