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 | # Rename _icon and _type options to allow overriding them in options
|
---|
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 | # In some Tcl installations, Tcl converts yes/no into a boolean
|
---|
74 | if isinstance(res, bool):
|
---|
75 | if res: return YES
|
---|
76 | return NO
|
---|
77 | return res
|
---|
78 |
|
---|
79 | def showinfo(title=None, message=None, **options):
|
---|
80 | "Show an info message"
|
---|
81 | return _show(title, message, INFO, OK, **options)
|
---|
82 |
|
---|
83 | def showwarning(title=None, message=None, **options):
|
---|
84 | "Show a warning message"
|
---|
85 | return _show(title, message, WARNING, OK, **options)
|
---|
86 |
|
---|
87 | def showerror(title=None, message=None, **options):
|
---|
88 | "Show an error message"
|
---|
89 | return _show(title, message, ERROR, OK, **options)
|
---|
90 |
|
---|
91 | def askquestion(title=None, message=None, **options):
|
---|
92 | "Ask a question"
|
---|
93 | return _show(title, message, QUESTION, YESNO, **options)
|
---|
94 |
|
---|
95 | def askokcancel(title=None, message=None, **options):
|
---|
96 | "Ask if operation should proceed; return true if the answer is ok"
|
---|
97 | s = _show(title, message, QUESTION, OKCANCEL, **options)
|
---|
98 | return s == OK
|
---|
99 |
|
---|
100 | def askyesno(title=None, message=None, **options):
|
---|
101 | "Ask a question; return true if the answer is yes"
|
---|
102 | s = _show(title, message, QUESTION, YESNO, **options)
|
---|
103 | return s == YES
|
---|
104 |
|
---|
105 | def askyesnocancel(title=None, message=None, **options):
|
---|
106 | "Ask a question; return true if the answer is yes, None if cancelled."
|
---|
107 | s = _show(title, message, QUESTION, YESNOCANCEL, **options)
|
---|
108 | # s might be a Tcl index object, so convert it to a string
|
---|
109 | s = str(s)
|
---|
110 | if s == CANCEL:
|
---|
111 | return None
|
---|
112 | return s == YES
|
---|
113 |
|
---|
114 | def askretrycancel(title=None, message=None, **options):
|
---|
115 | "Ask if operation should be retried; return true if the answer is yes"
|
---|
116 | s = _show(title, message, WARNING, RETRYCANCEL, **options)
|
---|
117 | return s == RETRY
|
---|
118 |
|
---|
119 |
|
---|
120 | # --------------------------------------------------------------------
|
---|
121 | # test stuff
|
---|
122 |
|
---|
123 | if __name__ == "__main__":
|
---|
124 |
|
---|
125 | print "info", showinfo("Spam", "Egg Information")
|
---|
126 | print "warning", showwarning("Spam", "Egg Warning")
|
---|
127 | print "error", showerror("Spam", "Egg Alert")
|
---|
128 | print "question", askquestion("Spam", "Question?")
|
---|
129 | print "proceed", askokcancel("Spam", "Proceed?")
|
---|
130 | print "yes/no", askyesno("Spam", "Got it?")
|
---|
131 | print "yes/no/cancel", askyesnocancel("Spam", "Want it?")
|
---|
132 | print "try again", askretrycancel("Spam", "Try again?")
|
---|