tkMessageBox.py 3.2 KB

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