Page 339 -
P. 339
declarative and interrogative messages
Creating message boxes in Python
All of the message box code is contained within a tkinter module called
messagebox, so the first thing to do is import the module:
import tkinter.messagebox
Then, you’re good to go. Within the messagebox module, there’s a whole
bunch of different dialogs to choose from. But all of them fall into two main
categories.
Message boxes that say stuff
To display a simple message on the screen, you might display a message box
like this:
The contents of the message.
tkinter.messagebox.showinfo("Delivery", "The cupcakes have arrived in Istanbul")
The title of the message box.
The icon in the window shows
that this is just for information.
You need to click the OK
button to close the dialog.
Message boxes that ask stuff
If you need a message box that asks the users a question, you will need to
check the return value to see what they chose:
response = tkinter.messagebox.askyesnocancel("Gift?", "Gift wrap the package?")
A value is assigned to “response” after
the user clicks one of the buttons.
When tkinter gets to this line, it will wait for the user to
answer the question and then assign True (yes), False
(no), or None (cancel) to the response variable.
Let’s see what other message boxes are
available.
304 Chapter 8 ½