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