Message Boxes and If Statements

Let's try out some more message box with out If Statements from the previous lesson.

You can either delete the code you have or start a new Python project for this. Now import the tkinter library. Remember, if you have version 3 of Python, you can just use this as the first line:

from tkinter import messagebox

If you have version 2, this should work:

import tkMessageBox

If it doesn't, you may need another import line:

import tkinter
import tkMessageBox

Add the underscore character, if you use a Mac with version 2 of Python:

import _tkinter
import tkMessageBox

In a previous section, we just displayed a message box with showinfo:

messagebox.showinfo("Title Here", "Message Here")

However, there are other types of message box you can use, and they can return values. The types of message boxes you can use to ask questions are these:

askokcancel
askretrycancel
askyesno
askyesnocancel

Let's see them in action.

If you have Python 3, try this code:

from tkinter import messagebox

response = messagebox.askokcancel("Ask Question", "OK or Cancel?")

print(response)

For Python 2 users, the code is slightly different:

import tkMessageBox

response = tkMessageBox.askokcancel("Ask Question", "OK or Cancel?")

print(response)

(NOTE: For the rest of these examples, we're just going to use the Python 3 message boxes. Just remember to amend your code, if you're using Python 2.)

Instead of the showinfo we used before, we now have this:

messagebox.askokcancel("Ask Question", "OK or Cancel?")

We're using askokcancel. This will get you an OK button and a Cancel button. In between round brackets, we have the usual title as the text for the message box, followed by a comma then the message to be used in the message box.

Notice what else is different. We have the message box code on the right of an equal sign. Whatever the user clicks, OK or Cancel, will get returned and placed in the variable that we have called response. What gets returned is either True or False.

Try it out. Run your code. You should see this, first:

A Tkinter OK/CANCEL message box

Click OK and what will appear in the output window is the word True. Run your code again and click Cancel. Now the output window will print False.

We can use an If Statement to test what is inside of the response variable. Change your code to this:

response = messagebox.askokcancel("Ask Question", "OK or Cancel?")

if response == True:

print("OK button clicked")

elif response == False:

print("Cancel button clicked")

(This is for Python 3 users. Python 2 users should change messagebox to tkMessageBox.)

With only two choices, something that isn't true will be false. So we can change the elif line to an else line:

if response == True:

print("OK button clicked")

else:

print("Cancel button clicked")

There is an even simpler way to test for True values. Change your If line to this:

if response:

print("OK button clicked")

else:

print("Cancel button clicked")

This should make any underlines go away in PyCharm. By just saying "if response" you're really telling Python to check for a value of True. You don't need to add the == True part because Python knows what you mean. However, it makes your code more readable if you include the == True at the end.

To check for false values, you can use the keyword not:

if not response:

print("It was false")

This checks to see if the value in response is false.

Python Exercise

Try the other message box methods:

askretrycancel
askyesno

Change your print statements to something suitable.


Now try the other message box, askyesnocancel. When this one displays, you'll find it has three choices. Yes, No, and Cancel:

YES, NO, CANCEL  message box in Tkinter

So what happens to the True or False options? Are No and Cancel both False?

Add a print line to your code, as the last line:

print(response)

Run your code and click the Cancel option. What you should see is the Word None printed to the output window. The possible values that can end up in the response variable are now True, False, and None. You can amend your if statement to catch all three options:

if response == True:

print("You clicked Yes")

elif response == False:

print("You clicked No")

elif response is None:

print("You clicked Cancel")

Notice that the test for the value None is done with the word is. You won't get an error if you use two equal signs:

elif response == None:

But it's usual to use the keyword is instead of the equal signs.

You can simplify your new If statement, if you want:

if response:

print("You clicked Yes")

elif not response:

print("You clicked No")

elif response is None:

print("You clicked Cancel")

You can also use a message box for your response, instead of printing it the output window. Try these.

if response:

messagebox.showinfo("Response", "You clicked Yes")

elif not response:

messagebox.showinfo("Response","You clicked No")

You can ask if your user is really sure with something like this:

response = messagebox.askokcancel("Ask Question", "OK or Cancel?")

if response:

certain = messagebox.askyesno("Certain?", "Are you sure you wish to proceed?")

if certain:

messagebox.showinfo("Proceed Status", "Going ahead")

else:

messagebox.showinfo("Proceed Status", "Cancelling")

elif not response:

messagebox.showinfo("Response","You clicked Cancel")

Rather than explain what the code does, try it out.

OK, we'll move on from If Statements. Let's go back to strings, because there's a lot more than meets the eye when it comes to strings of text. Having a good grasp of string manipulation will really improve your skills as a programmer.

Python String Manipulation >