messagebox.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 tkinter.commondialog import Dialog
  24. __all__ = ["showinfo", "showwarning", "showerror",
  25. "askquestion", "askokcancel", "askyesno",
  26. "askyesnocancel", "askretrycancel"]
  27. #
  28. # constants
  29. # icons
  30. ERROR = "error"
  31. INFO = "info"
  32. QUESTION = "question"
  33. WARNING = "warning"
  34. # types
  35. ABORTRETRYIGNORE = "abortretryignore"
  36. OK = "ok"
  37. OKCANCEL = "okcancel"
  38. RETRYCANCEL = "retrycancel"
  39. YESNO = "yesno"
  40. YESNOCANCEL = "yesnocancel"
  41. # replies
  42. ABORT = "abort"
  43. RETRY = "retry"
  44. IGNORE = "ignore"
  45. OK = "ok"
  46. CANCEL = "cancel"
  47. YES = "yes"
  48. NO = "no"
  49. #
  50. # message dialog class
  51. class Message(Dialog):
  52. "A message box"
  53. command = "tk_messageBox"
  54. #
  55. # convenience stuff
  56. # Rename _icon and _type options to allow overriding them in options
  57. def _show(title=None, message=None, _icon=None, _type=None, **options):
  58. if _icon and "icon" not in options: options["icon"] = _icon
  59. if _type and "type" not in options: 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, yes/no is converted into a boolean.
  64. if isinstance(res, bool):
  65. if res:
  66. return YES
  67. return NO
  68. # In others we get a Tcl_Obj.
  69. return str(res)
  70. def showinfo(title=None, message=None, **options):
  71. "Show an info message"
  72. return _show(title, message, INFO, OK, **options)
  73. def showwarning(title=None, message=None, **options):
  74. "Show a warning message"
  75. return _show(title, message, WARNING, OK, **options)
  76. def showerror(title=None, message=None, **options):
  77. "Show an error message"
  78. return _show(title, message, ERROR, OK, **options)
  79. def askquestion(title=None, message=None, **options):
  80. "Ask a question"
  81. return _show(title, message, QUESTION, YESNO, **options)
  82. def askokcancel(title=None, message=None, **options):
  83. "Ask if operation should proceed; return true if the answer is ok"
  84. s = _show(title, message, QUESTION, OKCANCEL, **options)
  85. return s == OK
  86. def askyesno(title=None, message=None, **options):
  87. "Ask a question; return true if the answer is yes"
  88. s = _show(title, message, QUESTION, YESNO, **options)
  89. return s == YES
  90. def askyesnocancel(title=None, message=None, **options):
  91. "Ask a question; return true if the answer is yes, None if cancelled."
  92. s = _show(title, message, QUESTION, YESNOCANCEL, **options)
  93. # s might be a Tcl index object, so convert it to a string
  94. s = str(s)
  95. if s == CANCEL:
  96. return None
  97. return s == YES
  98. def askretrycancel(title=None, message=None, **options):
  99. "Ask if operation should be retried; return true if the answer is yes"
  100. s = _show(title, message, WARNING, RETRYCANCEL, **options)
  101. return s == RETRY
  102. # --------------------------------------------------------------------
  103. # test stuff
  104. if __name__ == "__main__":
  105. print("info", showinfo("Spam", "Egg Information"))
  106. print("warning", showwarning("Spam", "Egg Warning"))
  107. print("error", showerror("Spam", "Egg Alert"))
  108. print("question", askquestion("Spam", "Question?"))
  109. print("proceed", askokcancel("Spam", "Proceed?"))
  110. print("yes/no", askyesno("Spam", "Got it?"))
  111. print("yes/no/cancel", askyesnocancel("Spam", "Want it?"))
  112. print("try again", askretrycancel("Spam", "Try again?"))