tkCommonDialog.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #
  2. # Instant Python
  3. # $Id: tkCommonDialog.py 32140 2003-04-06 09:01:11Z rhettinger $
  4. #
  5. # base class for tk common dialogues
  6. #
  7. # this module provides a base class for accessing the common
  8. # dialogues available in Tk 4.2 and newer. use tkFileDialog,
  9. # tkColorChooser, and tkMessageBox to access the individual
  10. # dialogs.
  11. #
  12. # written by Fredrik Lundh, May 1997
  13. #
  14. from Tkinter import *
  15. class Dialog:
  16. command = None
  17. def __init__(self, master=None, **options):
  18. # FIXME: should this be placed on the module level instead?
  19. if TkVersion < 4.2:
  20. raise TclError, "this module requires Tk 4.2 or newer"
  21. self.master = master
  22. self.options = options
  23. if not master and options.get('parent'):
  24. self.master = options['parent']
  25. def _fixoptions(self):
  26. pass # hook
  27. def _fixresult(self, widget, result):
  28. return result # hook
  29. def show(self, **options):
  30. # update instance options
  31. for k, v in options.items():
  32. self.options[k] = v
  33. self._fixoptions()
  34. # we need a dummy widget to properly process the options
  35. # (at least as long as we use Tkinter 1.63)
  36. w = Frame(self.master)
  37. try:
  38. s = w.tk.call(self.command, *w._options(self.options))
  39. s = self._fixresult(w, s)
  40. finally:
  41. try:
  42. # get rid of the widget
  43. w.destroy()
  44. except:
  45. pass
  46. return s