blob: 12e42fe14ac451074720f51b40bf291c88d62978 [file] [log] [blame]
Guido van Rossum1e8c8a21997-07-19 20:02:36 +00001# base class for tk common dialogues
2#
3# this module provides a base class for accessing the common
Georg Brandl14fc4272008-05-17 18:39:55 +00004# dialogues available in Tk 4.2 and newer. use filedialog,
5# colorchooser, and messagebox to access the individual
Guido van Rossum1e8c8a21997-07-19 20:02:36 +00006# dialogs.
7#
8# written by Fredrik Lundh, May 1997
9#
10
Flavian Hautbois76b64512019-07-26 03:30:33 +020011__all__ = ["Dialog"]
12
13from tkinter import Frame
Guido van Rossum1e8c8a21997-07-19 20:02:36 +000014
Serhiy Storchakadc0d5712018-10-12 19:01:00 +030015
Guido van Rossum1e8c8a21997-07-19 20:02:36 +000016class Dialog:
17
Flavian Hautbois76b64512019-07-26 03:30:33 +020018 command = None
Guido van Rossum1e8c8a21997-07-19 20:02:36 +000019
20 def __init__(self, master=None, **options):
Serhiy Storchakabb70b2a2020-12-25 17:04:26 +020021 if master is None:
Serhiy Storchaka3d569fd2020-12-19 12:17:08 +020022 master = options.get('parent')
Flavian Hautbois76b64512019-07-26 03:30:33 +020023 self.master = master
Guido van Rossum1e8c8a21997-07-19 20:02:36 +000024 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
Guido van Rossumc4570481998-03-20 20:45:49 +000035 for k, v in options.items():
36 self.options[k] = v
Guido van Rossum1e8c8a21997-07-19 20:02:36 +000037
38 self._fixoptions()
39
Guido van Rossum1530c871997-08-14 14:17:28 +000040 # we need a dummy widget to properly process the options
Guido van Rossum1e8c8a21997-07-19 20:02:36 +000041 # (at least as long as we use Tkinter 1.63)
42 w = Frame(self.master)
43
44 try:
45
Raymond Hettingerff41c482003-04-06 09:01:11 +000046 s = w.tk.call(self.command, *w._options(self.options))
Guido van Rossum1e8c8a21997-07-19 20:02:36 +000047
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