0001 # 0002 # Instant Python 0003 # $Id: tkMessageBox.py,v 1.3 2004/09/18 16:01:23 loewis Exp $ 0004 # 0005 # tk common message boxes 0006 # 0007 # this module provides an interface to the native message boxes 0008 # available in Tk 4.2 and newer. 0009 # 0010 # written by Fredrik Lundh, May 1997 0011 # 0012 0013 # 0014 # options (all have default values): 0015 # 0016 # - default: which button to make default (one of the reply codes) 0017 # 0018 # - icon: which icon to display (see below) 0019 # 0020 # - message: the message to display 0021 # 0022 # - parent: which window to place the dialog on top of 0023 # 0024 # - title: dialog title 0025 # 0026 # - type: dialog type; that is, which buttons to display (see below) 0027 # 0028 0029 from tkCommonDialog import Dialog 0030 0031 # 0032 # constants 0033 0034 # icons 0035 ERROR = "error" 0036 INFO = "info" 0037 QUESTION = "question" 0038 WARNING = "warning" 0039 0040 # types 0041 ABORTRETRYIGNORE = "abortretryignore" 0042 OK = "ok" 0043 OKCANCEL = "okcancel" 0044 RETRYCANCEL = "retrycancel" 0045 YESNO = "yesno" 0046 YESNOCANCEL = "yesnocancel" 0047 0048 # replies 0049 ABORT = "abort" 0050 RETRY = "retry" 0051 IGNORE = "ignore" 0052 OK = "ok" 0053 CANCEL = "cancel" 0054 YES = "yes" 0055 NO = "no" 0056 0057 0058 # 0059 # message dialog class 0060 0061 class Message(Dialog): 0062 "A message box" 0063 0064 command = "tk_messageBox" 0065 0066 0067 # 0068 # convenience stuff 0069 0070 def _show(title=None, message=None, icon=None, type=None, **options): 0071 if icon: options["icon"] = icon 0072 if type: options["type"] = type 0073 if title: options["title"] = title 0074 if message: options["message"] = message 0075 res = Message(**options).show() 0076 # In some Tcl installations, Tcl converts yes/no into a boolean 0077 if isinstance(res, bool): 0078 if res: return YES 0079 return NO 0080 return res 0081 0082 def showinfo(title=None, message=None, **options): 0083 "Show an info message" 0084 return _show(title, message, INFO, OK, **options) 0085 0086 def showwarning(title=None, message=None, **options): 0087 "Show a warning message" 0088 return _show(title, message, WARNING, OK, **options) 0089 0090 def showerror(title=None, message=None, **options): 0091 "Show an error message" 0092 return _show(title, message, ERROR, OK, **options) 0093 0094 def askquestion(title=None, message=None, **options): 0095 "Ask a question" 0096 return _show(title, message, QUESTION, YESNO, **options) 0097 0098 def askokcancel(title=None, message=None, **options): 0099 "Ask if operation should proceed; return true if the answer is ok" 0100 s = _show(title, message, QUESTION, OKCANCEL, **options) 0101 return s == OK 0102 0103 def askyesno(title=None, message=None, **options): 0104 "Ask a question; return true if the answer is yes" 0105 s = _show(title, message, QUESTION, YESNO, **options) 0106 return s == YES 0107 0108 def askretrycancel(title=None, message=None, **options): 0109 "Ask if operation should be retried; return true if the answer is yes" 0110 s = _show(title, message, WARNING, RETRYCANCEL, **options) 0111 return s == RETRY 0112 0113 0114 # -------------------------------------------------------------------- 0115 # test stuff 0116 0117 if __name__ == "__main__": 0118 0119 print "info", showinfo("Spam", "Egg Information") 0120 print "warning", showwarning("Spam", "Egg Warning") 0121 print "error", showerror("Spam", "Egg Alert") 0122 print "question", askquestion("Spam", "Question?") 0123 print "proceed", askokcancel("Spam", "Proceed?") 0124 print "yes/no", askyesno("Spam", "Got it?") 0125 print "try again", askretrycancel("Spam", "Try again?") 0126
Generated by PyXR 0.9.4