Guido van Rossum | 1e8c8a2 | 1997-07-19 20:02:36 +0000 | [diff] [blame] | 1 | # base class for tk common dialogues |
| 2 | # |
| 3 | # this module provides a base class for accessing the common |
Georg Brandl | 14fc427 | 2008-05-17 18:39:55 +0000 | [diff] [blame] | 4 | # dialogues available in Tk 4.2 and newer. use filedialog, |
| 5 | # colorchooser, and messagebox to access the individual |
Guido van Rossum | 1e8c8a2 | 1997-07-19 20:02:36 +0000 | [diff] [blame] | 6 | # dialogs. |
| 7 | # |
| 8 | # written by Fredrik Lundh, May 1997 |
| 9 | # |
| 10 | |
Georg Brandl | 14fc427 | 2008-05-17 18:39:55 +0000 | [diff] [blame] | 11 | from tkinter import * |
Guido van Rossum | 1e8c8a2 | 1997-07-19 20:02:36 +0000 | [diff] [blame] | 12 | |
Serhiy Storchaka | dc0d571 | 2018-10-12 19:01:00 +0300 | [diff] [blame] | 13 | |
Guido van Rossum | 1e8c8a2 | 1997-07-19 20:02:36 +0000 | [diff] [blame] | 14 | class Dialog: |
| 15 | |
| 16 | command = None |
| 17 | |
| 18 | def __init__(self, master=None, **options): |
Guido van Rossum | c457048 | 1998-03-20 20:45:49 +0000 | [diff] [blame] | 19 | self.master = master |
Guido van Rossum | 1e8c8a2 | 1997-07-19 20:02:36 +0000 | [diff] [blame] | 20 | self.options = options |
Guido van Rossum | 3179b36 | 1998-10-12 20:40:47 +0000 | [diff] [blame] | 21 | if not master and options.get('parent'): |
| 22 | self.master = options['parent'] |
Guido van Rossum | 1e8c8a2 | 1997-07-19 20:02:36 +0000 | [diff] [blame] | 23 | |
| 24 | def _fixoptions(self): |
| 25 | pass # hook |
| 26 | |
| 27 | def _fixresult(self, widget, result): |
| 28 | return result # hook |
| 29 | |
| 30 | def show(self, **options): |
| 31 | |
| 32 | # update instance options |
Guido van Rossum | c457048 | 1998-03-20 20:45:49 +0000 | [diff] [blame] | 33 | for k, v in options.items(): |
| 34 | self.options[k] = v |
Guido van Rossum | 1e8c8a2 | 1997-07-19 20:02:36 +0000 | [diff] [blame] | 35 | |
| 36 | self._fixoptions() |
| 37 | |
Guido van Rossum | 1530c87 | 1997-08-14 14:17:28 +0000 | [diff] [blame] | 38 | # we need a dummy widget to properly process the options |
Guido van Rossum | 1e8c8a2 | 1997-07-19 20:02:36 +0000 | [diff] [blame] | 39 | # (at least as long as we use Tkinter 1.63) |
| 40 | w = Frame(self.master) |
| 41 | |
| 42 | try: |
| 43 | |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 44 | s = w.tk.call(self.command, *w._options(self.options)) |
Guido van Rossum | 1e8c8a2 | 1997-07-19 20:02:36 +0000 | [diff] [blame] | 45 | |
| 46 | s = self._fixresult(w, s) |
| 47 | |
| 48 | finally: |
| 49 | |
| 50 | try: |
| 51 | # get rid of the widget |
| 52 | w.destroy() |
| 53 | except: |
| 54 | pass |
| 55 | |
| 56 | return s |