blob: 2a69d282638529d0fced3efe8d1d7d1d09044f56 [file] [log] [blame]
Ned Batchelderbcb435e2024-05-08 19:34:401:mod:`!tkinter.messagebox` --- Tkinter message prompts
2======================================================
Nikhil80428ed2019-09-10 08:55:343
4.. module:: tkinter.messagebox
Nikhil80428ed2019-09-10 08:55:345 :synopsis: Various types of alert dialogs
6
7**Source code:** :source:`Lib/tkinter/messagebox.py`
8
9--------------
10
Ned Batchelder638d22c2026-02-06 11:48:2711The :mod:`!tkinter.messagebox` module provides a template base class as well as
Nikhil80428ed2019-09-10 08:55:3412a variety of convenience methods for commonly used configurations. The message
Serhiy Storchakaeaf67e32023-11-01 10:27:0213boxes are modal and will return a subset of (``True``, ``False``, ``None``,
14:data:`OK`, :data:`CANCEL`, :data:`YES`, :data:`NO`) based on
Nikhil80428ed2019-09-10 08:55:3415the user's selection. Common message box styles and layouts include but are not
16limited to:
17
18.. figure:: tk_msg.png
19
20.. class:: Message(master=None, **options)
21
Serhiy Storchakaeaf67e32023-11-01 10:27:0222 Create a message window with an application-specified message, an icon
23 and a set of buttons.
24 Each of the buttons in the message window is identified by a unique symbolic name (see the *type* options).
25
26 The following options are supported:
27
28 *command*
29 Specifies the function to invoke when the user closes the dialog.
30 The name of the button clicked by the user to close the dialog is
31 passed as argument.
32 This is only available on macOS.
33
34 *default*
35 Gives the :ref:`symbolic name <messagebox-buttons>` of the default button
36 for this message window (:data:`OK`, :data:`CANCEL`, and so on).
37 If this option is not specified, the first button in the dialog will
38 be made the default.
39
40 *detail*
41 Specifies an auxiliary message to the main message given by the
42 *message* option.
43 The message detail will be presented beneath the main message and,
44 where supported by the OS, in a less emphasized font than the main
45 message.
46
47 *icon*
48 Specifies an :ref:`icon <messagebox-icons>` to display.
49 If this option is not specified, then the :data:`INFO` icon will be
50 displayed.
51
52 *message*
53 Specifies the message to display in this message box.
54 The default value is an empty string.
55
56 *parent*
57 Makes the specified window the logical parent of the message box.
58 The message box is displayed on top of its parent window.
59
60 *title*
61 Specifies a string to display as the title of the message box.
62 This option is ignored on macOS, where platform guidelines forbid
63 the use of a title on this kind of dialog.
64
65 *type*
66 Arranges for a :ref:`predefined set of buttons <messagebox-types>`
67 to be displayed.
68
69 .. method:: show(**options)
70
71 Display a message window and wait for the user to select one of the buttons. Then return the symbolic name of the selected button.
72 Keyword arguments can override options specified in the constructor.
73
Nikhil80428ed2019-09-10 08:55:3474
75**Information message box**
76
Serhiy Storchakaeaf67e32023-11-01 10:27:0277.. function:: showinfo(title=None, message=None, **options)
78
79 Creates and displays an information message box with the specified title
80 and message.
Nikhil80428ed2019-09-10 08:55:3481
82**Warning message boxes**
83
Serhiy Storchakaeaf67e32023-11-01 10:27:0284.. function:: showwarning(title=None, message=None, **options)
85
86 Creates and displays a warning message box with the specified title
87 and message.
88
89.. function:: showerror(title=None, message=None, **options)
90
91 Creates and displays an error message box with the specified title
92 and message.
Nikhil80428ed2019-09-10 08:55:3493
94**Question message boxes**
95
Serhiy Storchakaeaf67e32023-11-01 10:27:0296.. function:: askquestion(title=None, message=None, *, type=YESNO, **options)
97
98 Ask a question. By default shows buttons :data:`YES` and :data:`NO`.
99 Returns the symbolic name of the selected button.
100
101.. function:: askokcancel(title=None, message=None, **options)
102
103 Ask if operation should proceed. Shows buttons :data:`OK` and :data:`CANCEL`.
104 Returns ``True`` if the answer is ok and ``False`` otherwise.
105
106.. function:: askretrycancel(title=None, message=None, **options)
107
108 Ask if operation should be retried. Shows buttons :data:`RETRY` and :data:`CANCEL`.
109 Return ``True`` if the answer is yes and ``False`` otherwise.
110
111.. function:: askyesno(title=None, message=None, **options)
112
113 Ask a question. Shows buttons :data:`YES` and :data:`NO`.
114 Returns ``True`` if the answer is yes and ``False`` otherwise.
115
116.. function:: askyesnocancel(title=None, message=None, **options)
117
118 Ask a question. Shows buttons :data:`YES`, :data:`NO` and :data:`CANCEL`.
119 Return ``True`` if the answer is yes, ``None`` if cancelled, and ``False``
120 otherwise.
121
122
123.. _messagebox-buttons:
124
125Symbolic names of buttons:
126
127.. data:: ABORT
128 :value: 'abort'
129.. data:: RETRY
130 :value: 'retry'
131.. data:: IGNORE
132 :value: 'ignore'
133.. data:: OK
134 :value: 'ok'
135.. data:: CANCEL
136 :value: 'cancel'
137.. data:: YES
138 :value: 'yes'
139.. data:: NO
140 :value: 'no'
141
142.. _messagebox-types:
143
144Predefined sets of buttons:
145
146.. data:: ABORTRETRYIGNORE
147 :value: 'abortretryignore'
148
149 Displays three buttons whose symbolic names are :data:`ABORT`,
150 :data:`RETRY` and :data:`IGNORE`.
151
152.. data:: OK
153 :value: 'ok'
154 :noindex:
155
156 Displays one button whose symbolic name is :data:`OK`.
157
158.. data:: OKCANCEL
159 :value: 'okcancel'
160
161 Displays two buttons whose symbolic names are :data:`OK` and
162 :data:`CANCEL`.
163
164.. data:: RETRYCANCEL
165 :value: 'retrycancel'
166
167 Displays two buttons whose symbolic names are :data:`RETRY` and
168 :data:`CANCEL`.
169
170.. data:: YESNO
171 :value: 'yesno'
172
173 Displays two buttons whose symbolic names are :data:`YES` and
174 :data:`NO`.
175
176.. data:: YESNOCANCEL
177 :value: 'yesnocancel'
178
179 Displays three buttons whose symbolic names are :data:`YES`,
180 :data:`NO` and :data:`CANCEL`.
181
182.. _messagebox-icons:
183
184Icon images:
185
186.. data:: ERROR
187 :value: 'error'
188.. data:: INFO
189 :value: 'info'
190.. data:: QUESTION
191 :value: 'question'
192.. data:: WARNING
193 :value: 'warning'