/Lib/lib-tk/tkMessageBox.py

http://unladen-swallow.googlecode.com/ · Python · 132 lines · 71 code · 25 blank · 36 comment · 10 complexity · fcf1f9b077be68d5f705333d92a386d6 MD5 · raw file

  1. # tk common message boxes
  2. #
  3. # this module provides an interface to the native message boxes
  4. # available in Tk 4.2 and newer.
  5. #
  6. # written by Fredrik Lundh, May 1997
  7. #
  8. #
  9. # options (all have default values):
  10. #
  11. # - default: which button to make default (one of the reply codes)
  12. #
  13. # - icon: which icon to display (see below)
  14. #
  15. # - message: the message to display
  16. #
  17. # - parent: which window to place the dialog on top of
  18. #
  19. # - title: dialog title
  20. #
  21. # - type: dialog type; that is, which buttons to display (see below)
  22. #
  23. from tkCommonDialog import Dialog
  24. #
  25. # constants
  26. # icons
  27. ERROR = "error"
  28. INFO = "info"
  29. QUESTION = "question"
  30. WARNING = "warning"
  31. # types
  32. ABORTRETRYIGNORE = "abortretryignore"
  33. OK = "ok"
  34. OKCANCEL = "okcancel"
  35. RETRYCANCEL = "retrycancel"
  36. YESNO = "yesno"
  37. YESNOCANCEL = "yesnocancel"
  38. # replies
  39. ABORT = "abort"
  40. RETRY = "retry"
  41. IGNORE = "ignore"
  42. OK = "ok"
  43. CANCEL = "cancel"
  44. YES = "yes"
  45. NO = "no"
  46. #
  47. # message dialog class
  48. class Message(Dialog):
  49. "A message box"
  50. command = "tk_messageBox"
  51. #
  52. # convenience stuff
  53. # Rename _icon and _type options to allow overriding them in options
  54. def _show(title=None, message=None, _icon=None, _type=None, **options):
  55. if _icon and "icon" not in options: options["icon"] = _icon
  56. if _type and "type" not in options: options["type"] = _type
  57. if title: options["title"] = title
  58. if message: options["message"] = message
  59. res = Message(**options).show()
  60. # In some Tcl installations, Tcl converts yes/no into a boolean
  61. if isinstance(res, bool):
  62. if res: return YES
  63. return NO
  64. return res
  65. def showinfo(title=None, message=None, **options):
  66. "Show an info message"
  67. return _show(title, message, INFO, OK, **options)
  68. def showwarning(title=None, message=None, **options):
  69. "Show a warning message"
  70. return _show(title, message, WARNING, OK, **options)
  71. def showerror(title=None, message=None, **options):
  72. "Show an error message"
  73. return _show(title, message, ERROR, OK, **options)
  74. def askquestion(title=None, message=None, **options):
  75. "Ask a question"
  76. return _show(title, message, QUESTION, YESNO, **options)
  77. def askokcancel(title=None, message=None, **options):
  78. "Ask if operation should proceed; return true if the answer is ok"
  79. s = _show(title, message, QUESTION, OKCANCEL, **options)
  80. return s == OK
  81. def askyesno(title=None, message=None, **options):
  82. "Ask a question; return true if the answer is yes"
  83. s = _show(title, message, QUESTION, YESNO, **options)
  84. return s == YES
  85. def askyesnocancel(title=None, message=None, **options):
  86. "Ask a question; return true if the answer is yes, None if cancelled."
  87. s = _show(title, message, QUESTION, YESNOCANCEL, **options)
  88. # s might be a Tcl index object, so convert it to a string
  89. s = str(s)
  90. if s == CANCEL:
  91. return None
  92. return s == YES
  93. def askretrycancel(title=None, message=None, **options):
  94. "Ask if operation should be retried; return true if the answer is yes"
  95. s = _show(title, message, WARNING, RETRYCANCEL, **options)
  96. return s == RETRY
  97. # --------------------------------------------------------------------
  98. # test stuff
  99. if __name__ == "__main__":
  100. print "info", showinfo("Spam", "Egg Information")
  101. print "warning", showwarning("Spam", "Egg Warning")
  102. print "error", showerror("Spam", "Egg Alert")
  103. print "question", askquestion("Spam", "Question?")
  104. print "proceed", askokcancel("Spam", "Proceed?")
  105. print "yes/no", askyesno("Spam", "Got it?")
  106. print "yes/no/cancel", askyesnocancel("Spam", "Want it?")
  107. print "try again", askretrycancel("Spam", "Try again?")