Guido van Rossum | 1e8c8a2 | 1997-07-19 20:02:36 +0000 | [diff] [blame] | 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 | # |
| 10 | # options (all have default values): |
| 11 | # |
| 12 | # - default: which button to make default (one of the reply codes) |
| 13 | # |
| 14 | # - icon: which icon to display (see below) |
| 15 | # |
| 16 | # - message: the message to display |
| 17 | # |
| 18 | # - parent: which window to place the dialog on top of |
| 19 | # |
| 20 | # - title: dialog title |
| 21 | # |
| 22 | # - type: dialog type; that is, which buttons to display (see below) |
| 23 | # |
| 24 | |
| 25 | from tkCommonDialog import Dialog |
| 26 | |
| 27 | # |
| 28 | # constants |
| 29 | |
| 30 | # icons |
| 31 | ERROR = "error" |
| 32 | INFO = "info" |
| 33 | QUESTION = "question" |
| 34 | WARNING = "warning" |
| 35 | |
| 36 | # types |
| 37 | ABORTRETRYIGNORE = "abortretryignore" |
| 38 | OK = "ok" |
| 39 | OKCANCEL = "okcancel" |
| 40 | RETRYCANCEL = "retrycancel" |
| 41 | YESNO = "yesno" |
| 42 | YESNOCANCEL = "yesnocancel" |
| 43 | |
| 44 | # replies |
| 45 | ABORT = "abort" |
| 46 | RETRY = "retry" |
| 47 | IGNORE = "ignore" |
| 48 | OK = "ok" |
| 49 | CANCEL = "cancel" |
| 50 | YES = "yes" |
| 51 | NO = "no" |
| 52 | |
| 53 | |
| 54 | # |
| 55 | # message dialog class |
| 56 | |
| 57 | class Message(Dialog): |
| 58 | "A message box" |
| 59 | |
| 60 | command = "tk_messageBox" |
| 61 | |
| 62 | |
| 63 | # |
| 64 | # convenience stuff |
| 65 | |
| 66 | def _show(title=None, message=None, icon=None, type=None, **options): |
| 67 | if icon: options["icon"] = icon |
| 68 | if type: options["type"] = type |
| 69 | if title: options["title"] = title |
| 70 | if message: options["message"] = message |
Martin v. Löwis | b0c670c | 2004-09-18 16:01:23 +0000 | [diff] [blame] | 71 | res = Message(**options).show() |
| 72 | # In some Tcl installations, Tcl converts yes/no into a boolean |
| 73 | if isinstance(res, bool): |
| 74 | if res: return YES |
| 75 | return NO |
| 76 | return res |
Guido van Rossum | 1e8c8a2 | 1997-07-19 20:02:36 +0000 | [diff] [blame] | 77 | |
| 78 | def showinfo(title=None, message=None, **options): |
| 79 | "Show an info message" |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 80 | return _show(title, message, INFO, OK, **options) |
Guido van Rossum | 1e8c8a2 | 1997-07-19 20:02:36 +0000 | [diff] [blame] | 81 | |
| 82 | def showwarning(title=None, message=None, **options): |
| 83 | "Show a warning message" |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 84 | return _show(title, message, WARNING, OK, **options) |
Guido van Rossum | 1e8c8a2 | 1997-07-19 20:02:36 +0000 | [diff] [blame] | 85 | |
| 86 | def showerror(title=None, message=None, **options): |
| 87 | "Show an error message" |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 88 | return _show(title, message, ERROR, OK, **options) |
Guido van Rossum | 1e8c8a2 | 1997-07-19 20:02:36 +0000 | [diff] [blame] | 89 | |
| 90 | def askquestion(title=None, message=None, **options): |
| 91 | "Ask a question" |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 92 | return _show(title, message, QUESTION, YESNO, **options) |
Guido van Rossum | 1e8c8a2 | 1997-07-19 20:02:36 +0000 | [diff] [blame] | 93 | |
| 94 | def askokcancel(title=None, message=None, **options): |
| 95 | "Ask if operation should proceed; return true if the answer is ok" |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 96 | s = _show(title, message, QUESTION, OKCANCEL, **options) |
Guido van Rossum | 1e8c8a2 | 1997-07-19 20:02:36 +0000 | [diff] [blame] | 97 | return s == OK |
| 98 | |
| 99 | def askyesno(title=None, message=None, **options): |
| 100 | "Ask a question; return true if the answer is yes" |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 101 | s = _show(title, message, QUESTION, YESNO, **options) |
Guido van Rossum | 1e8c8a2 | 1997-07-19 20:02:36 +0000 | [diff] [blame] | 102 | return s == YES |
| 103 | |
| 104 | def askretrycancel(title=None, message=None, **options): |
| 105 | "Ask if operation should be retried; return true if the answer is yes" |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 106 | s = _show(title, message, WARNING, RETRYCANCEL, **options) |
Guido van Rossum | 1e8c8a2 | 1997-07-19 20:02:36 +0000 | [diff] [blame] | 107 | return s == RETRY |
| 108 | |
| 109 | |
| 110 | # -------------------------------------------------------------------- |
| 111 | # test stuff |
| 112 | |
| 113 | if __name__ == "__main__": |
| 114 | |
| 115 | print "info", showinfo("Spam", "Egg Information") |
| 116 | print "warning", showwarning("Spam", "Egg Warning") |
| 117 | print "error", showerror("Spam", "Egg Alert") |
| 118 | print "question", askquestion("Spam", "Question?") |
| 119 | print "proceed", askokcancel("Spam", "Proceed?") |
| 120 | print "yes/no", askyesno("Spam", "Got it?") |
| 121 | print "try again", askretrycancel("Spam", "Try again?") |