tkCommonDialog.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 tkFileDialog,
  5. # tkColorChooser, and tkMessageBox 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. # FIXME: should this be placed on the module level instead?
  15. if TkVersion < 4.2:
  16. raise TclError, "this module requires Tk 4.2 or newer"
  17. self.master = master
  18. self.options = options
  19. if not master and options.get('parent'):
  20. self.master = options['parent']
  21. def _fixoptions(self):
  22. pass # hook
  23. def _fixresult(self, widget, result):
  24. return result # hook
  25. def show(self, **options):
  26. # update instance options
  27. for k, v in options.items():
  28. self.options[k] = v
  29. self._fixoptions()
  30. # we need a dummy widget to properly process the options
  31. # (at least as long as we use Tkinter 1.63)
  32. w = Frame(self.master)
  33. try:
  34. s = w.tk.call(self.command, *w._options(self.options))
  35. s = self._fixresult(w, s)
  36. finally:
  37. try:
  38. # get rid of the widget
  39. w.destroy()
  40. except:
  41. pass
  42. return s