commondialog.py 1.2 KB

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