| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
from tkCommonDialog import Dialog |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
ERROR = "error" |
| 32 |
INFO = "info" |
| 33 |
QUESTION = "question" |
| 34 |
WARNING = "warning" |
| 35 |
|
| 36 |
|
| 37 |
ABORTRETRYIGNORE = "abortretryignore" |
| 38 |
OK = "ok" |
| 39 |
OKCANCEL = "okcancel" |
| 40 |
RETRYCANCEL = "retrycancel" |
| 41 |
YESNO = "yesno" |
| 42 |
YESNOCANCEL = "yesnocancel" |
| 43 |
|
| 44 |
|
| 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 |
|
| 56 |
|
| 57 |
class Message(Dialog): |
| 58 |
"A message box" |
| 59 |
|
| 60 |
command = "tk_messageBox" |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
def _show(title=None, message=None, _icon=None, _type=None, **options): |
| 68 |
if _icon and "icon" not in options: options["icon"] = _icon |
| 69 |
if _type and "type" not in options: options["type"] = _type |
| 70 |
if title: options["title"] = title |
| 71 |
if message: options["message"] = message |
| 72 |
res = Message(**options).show() |
| 73 |
|
| 74 |
if isinstance(res, bool): |
| 75 |
if res: |
| 76 |
return YES |
| 77 |
return NO |
| 78 |
|
| 79 |
return str(res) |
| 80 |
|
| 81 |
def showinfo(title=None, message=None, **options): |
| 82 |
"Show an info message" |
| 83 |
return _show(title, message, INFO, OK, **options) |
| 84 |
|
| 85 |
def showwarning(title=None, message=None, **options): |
| 86 |
"Show a warning message" |
| 87 |
return _show(title, message, WARNING, OK, **options) |
| 88 |
|
| 89 |
def showerror(title=None, message=None, **options): |
| 90 |
"Show an error message" |
| 91 |
return _show(title, message, ERROR, OK, **options) |
| 92 |
|
| 93 |
def askquestion(title=None, message=None, **options): |
| 94 |
"Ask a question" |
| 95 |
return _show(title, message, QUESTION, YESNO, **options) |
| 96 |
|
| 97 |
def askokcancel(title=None, message=None, **options): |
| 98 |
"Ask if operation should proceed; return true if the answer is ok" |
| 99 |
s = _show(title, message, QUESTION, OKCANCEL, **options) |
| 100 |
return s == OK |
| 101 |
|
| 102 |
def askyesno(title=None, message=None, **options): |
| 103 |
"Ask a question; return true if the answer is yes" |
| 104 |
s = _show(title, message, QUESTION, YESNO, **options) |
| 105 |
return s == YES |
| 106 |
|
| 107 |
def askyesnocancel(title=None, message=None, **options): |
| 108 |
"Ask a question; return true if the answer is yes, None if cancelled." |
| 109 |
s = _show(title, message, QUESTION, YESNOCANCEL, **options) |
| 110 |
|
| 111 |
s = str(s) |
| 112 |
if s == CANCEL: |
| 113 |
return None |
| 114 |
return s == YES |
| 115 |
|
| 116 |
def askretrycancel(title=None, message=None, **options): |
| 117 |
"Ask if operation should be retried; return true if the answer is yes" |
| 118 |
s = _show(title, message, WARNING, RETRYCANCEL, **options) |
| 119 |
return s == RETRY |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
if __name__ == "__main__": |
| 126 |
|
| 127 |
print "info", showinfo("Spam", "Egg Information") |
| 128 |
print "warning", showwarning("Spam", "Egg Warning") |
| 129 |
print "error", showerror("Spam", "Egg Alert") |
| 130 |
print "question", askquestion("Spam", "Question?") |
| 131 |
print "proceed", askokcancel("Spam", "Proceed?") |
| 132 |
print "yes/no", askyesno("Spam", "Got it?") |
| 133 |
print "yes/no/cancel", askyesnocancel("Spam", "Want it?") |
| 134 |
print "try again", askretrycancel("Spam", "Try again?") |