tkMessageBox.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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, yes/no is converted into a boolean.
  61. if isinstance(res, bool):
  62. if res:
  63. return YES
  64. return NO
  65. # In others we get a Tcl_Obj.
  66. return str(res)
  67. def showinfo(title=None, message=None, **options):
  68. "Show an info message"
  69. return _show(title, message, INFO, OK, **options)
  70. def showwarning(title=None, message=None, **options):
  71. "Show a warning message"
  72. return _show(title, message, WARNING, OK, **options)
  73. def showerror(title=None, message=None, **options):
  74. "Show an error message"
  75. return _show(title, message, ERROR, OK, **options)
  76. def askquestion(title=None, message=None, **options):
  77. "Ask a question"
  78. return _show(title, message, QUESTION, YESNO, **options)
  79. def askokcancel(title=None, message=None, **options):
  80. "Ask if operation should proceed; return true if the answer is ok"
  81. s = _show(title, message, QUESTION, OKCANCEL, **options)
  82. return s == OK
  83. def askyesno(title=None, message=None, **options):
  84. "Ask a question; return true if the answer is yes"
  85. s = _show(title, message, QUESTION, YESNO, **options)
  86. return s == YES
  87. def askyesnocancel(title=None, message=None, **options):
  88. "Ask a question; return true if the answer is yes, None if cancelled."
  89. s = _show(title, message, QUESTION, YESNOCANCEL, **options)
  90. # s might be a Tcl index object, so convert it to a string
  91. s = str(s)
  92. if s == CANCEL:
  93. return None
  94. return s == YES
  95. def askretrycancel(title=None, message=None, **options):
  96. "Ask if operation should be retried; return true if the answer is yes"
  97. s = _show(title, message, WARNING, RETRYCANCEL, **options)
  98. return s == RETRY
  99. # --------------------------------------------------------------------
  100. # test stuff
  101. if __name__ == "__main__":
  102. print "info", showinfo("Spam", "Egg Information")
  103. print "warning", showwarning("Spam", "Egg Warning")
  104. print "error", showerror("Spam", "Egg Alert")
  105. print "question", askquestion("Spam", "Question?")
  106. print "proceed", askokcancel("Spam", "Proceed?")
  107. print "yes/no", askyesno("Spam", "Got it?")
  108. print "yes/no/cancel", askyesnocancel("Spam", "Want it?")
  109. print "try again", askretrycancel("Spam", "Try again?")