blob: 4a711fa623b395eae6fcc7863d13e28c0def8d2c [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
27#
28# constants
29
30# icons
31ERROR = "error"
32INFO = "info"
33QUESTION = "question"
34WARNING = "warning"
35
36# types
37ABORTRETRYIGNORE = "abortretryignore"
38OK = "ok"
39OKCANCEL = "okcancel"
40RETRYCANCEL = "retrycancel"
41YESNO = "yesno"
42YESNOCANCEL = "yesnocancel"
43
44# replies
45ABORT = "abort"
46RETRY = "retry"
47IGNORE = "ignore"
48OK = "ok"
49CANCEL = "cancel"
50YES = "yes"
51NO = "no"
52
53
54#
55# message dialog class
56
57class Message(Dialog):
58 "A message box"
59
60 command = "tk_messageBox"
61
62
63#
64# convenience stuff
65
Thomas Wouters0e3f5912006-08-11 14:57:12 +000066# Rename _icon and _type options to allow overriding them in options
67def _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
Guido van Rossum1e8c8a21997-07-19 20:02:36 +000070 if title: options["title"] = title
71 if message: options["message"] = message
Martin v. Löwisb0c670c2004-09-18 16:01:23 +000072 res = Message(**options).show()
Matthias Klosea6d9abf2010-03-16 10:51:28 +000073 # In some Tcl installations, yes/no is converted into a boolean.
Martin v. Löwisb0c670c2004-09-18 16:01:23 +000074 if isinstance(res, bool):
Matthias Klosea6d9abf2010-03-16 10:51:28 +000075 if res:
76 return YES
Martin v. Löwisb0c670c2004-09-18 16:01:23 +000077 return NO
Matthias Klosea6d9abf2010-03-16 10:51:28 +000078 # In others we get a Tcl_Obj.
79 return str(res)
Guido van Rossum1e8c8a21997-07-19 20:02:36 +000080
Serhiy Storchakadc0d5712018-10-12 19:01:00 +030081
Guido van Rossum1e8c8a21997-07-19 20:02:36 +000082def showinfo(title=None, message=None, **options):
83 "Show an info message"
Raymond Hettingerff41c482003-04-06 09:01:11 +000084 return _show(title, message, INFO, OK, **options)
Guido van Rossum1e8c8a21997-07-19 20:02:36 +000085
Serhiy Storchakadc0d5712018-10-12 19:01:00 +030086
Guido van Rossum1e8c8a21997-07-19 20:02:36 +000087def showwarning(title=None, message=None, **options):
88 "Show a warning message"
Raymond Hettingerff41c482003-04-06 09:01:11 +000089 return _show(title, message, WARNING, OK, **options)
Guido van Rossum1e8c8a21997-07-19 20:02:36 +000090
Serhiy Storchakadc0d5712018-10-12 19:01:00 +030091
Guido van Rossum1e8c8a21997-07-19 20:02:36 +000092def showerror(title=None, message=None, **options):
93 "Show an error message"
Raymond Hettingerff41c482003-04-06 09:01:11 +000094 return _show(title, message, ERROR, OK, **options)
Guido van Rossum1e8c8a21997-07-19 20:02:36 +000095
Serhiy Storchakadc0d5712018-10-12 19:01:00 +030096
Guido van Rossum1e8c8a21997-07-19 20:02:36 +000097def askquestion(title=None, message=None, **options):
98 "Ask a question"
Raymond Hettingerff41c482003-04-06 09:01:11 +000099 return _show(title, message, QUESTION, YESNO, **options)
Guido van Rossum1e8c8a21997-07-19 20:02:36 +0000100
Serhiy Storchakadc0d5712018-10-12 19:01:00 +0300101
Guido van Rossum1e8c8a21997-07-19 20:02:36 +0000102def askokcancel(title=None, message=None, **options):
103 "Ask if operation should proceed; return true if the answer is ok"
Raymond Hettingerff41c482003-04-06 09:01:11 +0000104 s = _show(title, message, QUESTION, OKCANCEL, **options)
Guido van Rossum1e8c8a21997-07-19 20:02:36 +0000105 return s == OK
106
Serhiy Storchakadc0d5712018-10-12 19:01:00 +0300107
Guido van Rossum1e8c8a21997-07-19 20:02:36 +0000108def askyesno(title=None, message=None, **options):
109 "Ask a question; return true if the answer is yes"
Raymond Hettingerff41c482003-04-06 09:01:11 +0000110 s = _show(title, message, QUESTION, YESNO, **options)
Guido van Rossum1e8c8a21997-07-19 20:02:36 +0000111 return s == YES
112
Serhiy Storchakadc0d5712018-10-12 19:01:00 +0300113
Thomas Wouters89f507f2006-12-13 04:49:30 +0000114def askyesnocancel(title=None, message=None, **options):
115 "Ask a question; return true if the answer is yes, None if cancelled."
116 s = _show(title, message, QUESTION, YESNOCANCEL, **options)
117 # s might be a Tcl index object, so convert it to a string
118 s = str(s)
119 if s == CANCEL:
120 return None
121 return s == YES
122
Serhiy Storchakadc0d5712018-10-12 19:01:00 +0300123
Guido van Rossum1e8c8a21997-07-19 20:02:36 +0000124def askretrycancel(title=None, message=None, **options):
125 "Ask if operation should be retried; return true if the answer is yes"
Raymond Hettingerff41c482003-04-06 09:01:11 +0000126 s = _show(title, message, WARNING, RETRYCANCEL, **options)
Guido van Rossum1e8c8a21997-07-19 20:02:36 +0000127 return s == RETRY
128
129
130# --------------------------------------------------------------------
131# test stuff
132
133if __name__ == "__main__":
134
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000135 print("info", showinfo("Spam", "Egg Information"))
136 print("warning", showwarning("Spam", "Egg Warning"))
137 print("error", showerror("Spam", "Egg Alert"))
138 print("question", askquestion("Spam", "Question?"))
139 print("proceed", askokcancel("Spam", "Proceed?"))
140 print("yes/no", askyesno("Spam", "Got it?"))
141 print("yes/no/cancel", askyesnocancel("Spam", "Want it?"))
142 print("try again", askretrycancel("Spam", "Try again?"))