blob: 5f0343b660c68c5a884f38ca78291caf14936231 [file] [log] [blame]
Guido van Rossum1e8c8a21997-07-19 20:02:36 +00001# 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
Georg Brandl14fc4272008-05-17 18:39:55 +000025from tkinter.commondialog import Dialog
Guido van Rossum1e8c8a21997-07-19 20:02:36 +000026
Flavian Hautbois76b64512019-07-26 03:30:33 +020027__all__ = ["showinfo", "showwarning", "showerror",
28 "askquestion", "askokcancel", "askyesno",
29 "askyesnocancel", "askretrycancel"]
30
Guido van Rossum1e8c8a21997-07-19 20:02:36 +000031#
32# constants
33
34# icons
35ERROR = "error"
36INFO = "info"
37QUESTION = "question"
38WARNING = "warning"
39
40# types
41ABORTRETRYIGNORE = "abortretryignore"
42OK = "ok"
43OKCANCEL = "okcancel"
44RETRYCANCEL = "retrycancel"
45YESNO = "yesno"
46YESNOCANCEL = "yesnocancel"
47
48# replies
49ABORT = "abort"
50RETRY = "retry"
51IGNORE = "ignore"
52OK = "ok"
53CANCEL = "cancel"
54YES = "yes"
55NO = "no"
56
57
58#
59# message dialog class
60
61class Message(Dialog):
62 "A message box"
63
64 command = "tk_messageBox"
65
66
67#
68# convenience stuff
69
Thomas Wouters0e3f5912006-08-11 14:57:12 +000070# Rename _icon and _type options to allow overriding them in options
71def _show(title=None, message=None, _icon=None, _type=None, **options):
72 if _icon and "icon" not in options: options["icon"] = _icon
73 if _type and "type" not in options: options["type"] = _type
Guido van Rossum1e8c8a21997-07-19 20:02:36 +000074 if title: options["title"] = title
75 if message: options["message"] = message
Martin v. Löwisb0c670c2004-09-18 16:01:23 +000076 res = Message(**options).show()
Matthias Klosea6d9abf2010-03-16 10:51:28 +000077 # In some Tcl installations, yes/no is converted into a boolean.
Martin v. Löwisb0c670c2004-09-18 16:01:23 +000078 if isinstance(res, bool):
Matthias Klosea6d9abf2010-03-16 10:51:28 +000079 if res:
80 return YES
Martin v. Löwisb0c670c2004-09-18 16:01:23 +000081 return NO
Matthias Klosea6d9abf2010-03-16 10:51:28 +000082 # In others we get a Tcl_Obj.
83 return str(res)
Guido van Rossum1e8c8a21997-07-19 20:02:36 +000084
Serhiy Storchakadc0d5712018-10-12 19:01:00 +030085
Guido van Rossum1e8c8a21997-07-19 20:02:36 +000086def showinfo(title=None, message=None, **options):
87 "Show an info message"
Raymond Hettingerff41c482003-04-06 09:01:11 +000088 return _show(title, message, INFO, OK, **options)
Guido van Rossum1e8c8a21997-07-19 20:02:36 +000089
Serhiy Storchakadc0d5712018-10-12 19:01:00 +030090
Guido van Rossum1e8c8a21997-07-19 20:02:36 +000091def showwarning(title=None, message=None, **options):
92 "Show a warning message"
Raymond Hettingerff41c482003-04-06 09:01:11 +000093 return _show(title, message, WARNING, OK, **options)
Guido van Rossum1e8c8a21997-07-19 20:02:36 +000094
Serhiy Storchakadc0d5712018-10-12 19:01:00 +030095
Guido van Rossum1e8c8a21997-07-19 20:02:36 +000096def showerror(title=None, message=None, **options):
97 "Show an error message"
Raymond Hettingerff41c482003-04-06 09:01:11 +000098 return _show(title, message, ERROR, OK, **options)
Guido van Rossum1e8c8a21997-07-19 20:02:36 +000099
Serhiy Storchakadc0d5712018-10-12 19:01:00 +0300100
Guido van Rossum1e8c8a21997-07-19 20:02:36 +0000101def askquestion(title=None, message=None, **options):
102 "Ask a question"
Raymond Hettingerff41c482003-04-06 09:01:11 +0000103 return _show(title, message, QUESTION, YESNO, **options)
Guido van Rossum1e8c8a21997-07-19 20:02:36 +0000104
Serhiy Storchakadc0d5712018-10-12 19:01:00 +0300105
Guido van Rossum1e8c8a21997-07-19 20:02:36 +0000106def askokcancel(title=None, message=None, **options):
107 "Ask if operation should proceed; return true if the answer is ok"
Raymond Hettingerff41c482003-04-06 09:01:11 +0000108 s = _show(title, message, QUESTION, OKCANCEL, **options)
Guido van Rossum1e8c8a21997-07-19 20:02:36 +0000109 return s == OK
110
Serhiy Storchakadc0d5712018-10-12 19:01:00 +0300111
Guido van Rossum1e8c8a21997-07-19 20:02:36 +0000112def askyesno(title=None, message=None, **options):
113 "Ask a question; return true if the answer is yes"
Raymond Hettingerff41c482003-04-06 09:01:11 +0000114 s = _show(title, message, QUESTION, YESNO, **options)
Guido van Rossum1e8c8a21997-07-19 20:02:36 +0000115 return s == YES
116
Serhiy Storchakadc0d5712018-10-12 19:01:00 +0300117
Thomas Wouters89f507f2006-12-13 04:49:30 +0000118def askyesnocancel(title=None, message=None, **options):
119 "Ask a question; return true if the answer is yes, None if cancelled."
120 s = _show(title, message, QUESTION, YESNOCANCEL, **options)
121 # s might be a Tcl index object, so convert it to a string
122 s = str(s)
123 if s == CANCEL:
124 return None
125 return s == YES
126
Serhiy Storchakadc0d5712018-10-12 19:01:00 +0300127
Guido van Rossum1e8c8a21997-07-19 20:02:36 +0000128def askretrycancel(title=None, message=None, **options):
129 "Ask if operation should be retried; return true if the answer is yes"
Raymond Hettingerff41c482003-04-06 09:01:11 +0000130 s = _show(title, message, WARNING, RETRYCANCEL, **options)
Guido van Rossum1e8c8a21997-07-19 20:02:36 +0000131 return s == RETRY
132
133
134# --------------------------------------------------------------------
135# test stuff
136
137if __name__ == "__main__":
138
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000139 print("info", showinfo("Spam", "Egg Information"))
140 print("warning", showwarning("Spam", "Egg Warning"))
141 print("error", showerror("Spam", "Egg Alert"))
142 print("question", askquestion("Spam", "Question?"))
143 print("proceed", askokcancel("Spam", "Proceed?"))
144 print("yes/no", askyesno("Spam", "Got it?"))
145 print("yes/no/cancel", askyesnocancel("Spam", "Want it?"))
146 print("try again", askretrycancel("Spam", "Try again?"))