commondialog.py 1.2 KB

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