ยปCore Development>Code coverage>Lib/tkinter/messagebox.py

Python code coverage for Lib/tkinter/messagebox.py

#countcontent
1n/a# tk common message boxes
2n/a#
3n/a# this module provides an interface to the native message boxes
4n/a# available in Tk 4.2 and newer.
5n/a#
6n/a# written by Fredrik Lundh, May 1997
7n/a#
8n/a
9n/a#
10n/a# options (all have default values):
11n/a#
12n/a# - default: which button to make default (one of the reply codes)
13n/a#
14n/a# - icon: which icon to display (see below)
15n/a#
16n/a# - message: the message to display
17n/a#
18n/a# - parent: which window to place the dialog on top of
19n/a#
20n/a# - title: dialog title
21n/a#
22n/a# - type: dialog type; that is, which buttons to display (see below)
23n/a#
24n/a
25n/afrom tkinter.commondialog import Dialog
26n/a
27n/a#
28n/a# constants
29n/a
30n/a# icons
31n/aERROR = "error"
32n/aINFO = "info"
33n/aQUESTION = "question"
34n/aWARNING = "warning"
35n/a
36n/a# types
37n/aABORTRETRYIGNORE = "abortretryignore"
38n/aOK = "ok"
39n/aOKCANCEL = "okcancel"
40n/aRETRYCANCEL = "retrycancel"
41n/aYESNO = "yesno"
42n/aYESNOCANCEL = "yesnocancel"
43n/a
44n/a# replies
45n/aABORT = "abort"
46n/aRETRY = "retry"
47n/aIGNORE = "ignore"
48n/aOK = "ok"
49n/aCANCEL = "cancel"
50n/aYES = "yes"
51n/aNO = "no"
52n/a
53n/a
54n/a#
55n/a# message dialog class
56n/a
57n/aclass Message(Dialog):
58n/a "A message box"
59n/a
60n/a command = "tk_messageBox"
61n/a
62n/a
63n/a#
64n/a# convenience stuff
65n/a
66n/a# Rename _icon and _type options to allow overriding them in options
67n/adef _show(title=None, message=None, _icon=None, _type=None, **options):
68n/a if _icon and "icon" not in options: options["icon"] = _icon
69n/a if _type and "type" not in options: options["type"] = _type
70n/a if title: options["title"] = title
71n/a if message: options["message"] = message
72n/a res = Message(**options).show()
73n/a # In some Tcl installations, yes/no is converted into a boolean.
74n/a if isinstance(res, bool):
75n/a if res:
76n/a return YES
77n/a return NO
78n/a # In others we get a Tcl_Obj.
79n/a return str(res)
80n/a
81n/adef showinfo(title=None, message=None, **options):
82n/a "Show an info message"
83n/a return _show(title, message, INFO, OK, **options)
84n/a
85n/adef showwarning(title=None, message=None, **options):
86n/a "Show a warning message"
87n/a return _show(title, message, WARNING, OK, **options)
88n/a
89n/adef showerror(title=None, message=None, **options):
90n/a "Show an error message"
91n/a return _show(title, message, ERROR, OK, **options)
92n/a
93n/adef askquestion(title=None, message=None, **options):
94n/a "Ask a question"
95n/a return _show(title, message, QUESTION, YESNO, **options)
96n/a
97n/adef askokcancel(title=None, message=None, **options):
98n/a "Ask if operation should proceed; return true if the answer is ok"
99n/a s = _show(title, message, QUESTION, OKCANCEL, **options)
100n/a return s == OK
101n/a
102n/adef askyesno(title=None, message=None, **options):
103n/a "Ask a question; return true if the answer is yes"
104n/a s = _show(title, message, QUESTION, YESNO, **options)
105n/a return s == YES
106n/a
107n/adef askyesnocancel(title=None, message=None, **options):
108n/a "Ask a question; return true if the answer is yes, None if cancelled."
109n/a s = _show(title, message, QUESTION, YESNOCANCEL, **options)
110n/a # s might be a Tcl index object, so convert it to a string
111n/a s = str(s)
112n/a if s == CANCEL:
113n/a return None
114n/a return s == YES
115n/a
116n/adef askretrycancel(title=None, message=None, **options):
117n/a "Ask if operation should be retried; return true if the answer is yes"
118n/a s = _show(title, message, WARNING, RETRYCANCEL, **options)
119n/a return s == RETRY
120n/a
121n/a
122n/a# --------------------------------------------------------------------
123n/a# test stuff
124n/a
125n/aif __name__ == "__main__":
126n/a
127n/a print("info", showinfo("Spam", "Egg Information"))
128n/a print("warning", showwarning("Spam", "Egg Warning"))
129n/a print("error", showerror("Spam", "Egg Alert"))
130n/a print("question", askquestion("Spam", "Question?"))
131n/a print("proceed", askokcancel("Spam", "Proceed?"))
132n/a print("yes/no", askyesno("Spam", "Got it?"))
133n/a print("yes/no/cancel", askyesnocancel("Spam", "Want it?"))
134n/a print("try again", askretrycancel("Spam", "Try again?"))