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