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

' ' The contents of the message.

tkinter.messagebox.showinfo("Delivery", "The cupcakes have arrived in Istanbul")

The title of the message box.

The title of the message box.

Delivery

You need to click the i'

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.

B

7 ElMs!

(p ^

«tapihe pacKaae?

py&s

WO J 1 cancel"]

These are the message boxes available in tkinter. Think carefully about each of the following examples. We know which we'd use at Head First Labs. Which type of box on the left would you choose for each of the messages on the right? Connect the messages to the boxes with lines.

showinfo

"OK to fire boosters?"

showwarning

"Your tartan clogs have arrived."

showerror

"Seriously, I think he's just ignoring the phone."

askquestion

"'Danger, Will Robinson!"

askokcancel

"Do you want fries with that?"

askyesnocancel

"'Dude, the printer's busted."

askretrycancel

"'So, you want Nutella on your bacon and jelly sandwich?"

mixed messages

showinfo showwarning showerror

These are the message boxes available in tkinter. You were to think carefully about each of the following examples, then indicate which type of box on the left you would choose for each of the messages on the right. You were to connect the messages to the boxes with lines.

Are you REALLY sure you want to continue and do this thing? It's your last chance to change yow mind.

"OK to fire boosters?"

This is pure information. Nothing to worry about. Except the risk of ^-clashing with your velvet pixie hood.

"Your tartan clogs have arrived."

It didn't work last time, but if you like, y°u can try again.

"Seriously, I think he's just ignoring the phone."

askquestion askokcancel askyesnocancel askretrycancel showwarning showerror askquestion askokcancel askyesnocancel askretrycancel

'Dude, the printer's busted."

.„. OK, so there's nothing actually 'Danger, Will Robinson!" broken YET, but BE CAREFUL

YoU are going to continue, but ^ do you want this extra option?

'Do you want fries with that?"

Stuff's broken. You need to know.

'Dude, the printer's busted."

Do you want this additional opW, or would you like to forget about the whole thing?

So, you want Nutella on your bacon and jelly sandwich?"

Did your answers match ours? They might not. Selecting which type f message box to use depends a lot upon the particular program you're writing and how serious you think a decision i

The folks at Head-Ex want your program to display this message box if there's a problem saving a record to the deliveries . txt file:

fcrrori

LErrnO 1^1 PerrrMüLOn denied? 'dushvet lec^ixV

OK J

Complete the missing lines in this section of your program to create the message box Hint: You need to include a newline in the message box text.-------

from tkinter import

fileD = open("deliveries.txt", "a") fileD.write("Depot:\n") fileD.write("%s\n" % depot.get()) fileD.write("Description:\n") fileD.write("%s\n" % description.get()) fileD.write("Address:\n")

fileD.write("%s\n" % address.get("1.0", END)) depot.set("")

description.delete(0, END) description.delete(0, END) address.delete("1.0", END) except Exception as ex:

get the message r ^terpen your pencil k Solution

The folks at Head-Ex want your program to display this message box if there's a problem saving a record to the deliveries . txt file:

fcrror! EBil

Cen't wfike ko the^ie L£irnû Bj Perrnisiioo denied; 'deh'jef lec^ixV

OK J

Complete the missing lines in this section of your program to create the message box. Hint: You need to include a newline in the message box text.

from tkinter import * import tkinter.messagebox

Remember to

imPovt "the

^tessavy module- def save_data(): try:

fileD=open("deliveries.txt", "a") fileD.write("Depot\n") fileD.write("%s\n" % depot.get()) fileD.write("Description\n") fileD.write("%s\n" % description.get()) fileD.write("Address\n")

fileD.write("%s\n" % address.get("1.0", END)) depot.set("")

description.delete(0, END)

description.delete(0, END)

address.delete("1.0", END)

except Exception as ex:

tkrnter.messagebox.showerror(llError'", "Can't write to the file\n %s" % ex)

You should use the showerror() function so that the dialog box gets the correct error icon.

Tqst DriI/S

Now what happens if you try to save a record when the deliveries . txt file is read-only?

Head-Ex Deliveries

Depot: Rome, Italy -Description:

Bucket of Parmesan

Address:

The Holy See Suppliers, Inc. 42 Vatican Way Rome, Italy

This is an ERROR message box.

The i£on shows the user that something has gone wrong.

Can't write to the file [Errno 13] Permission denied: 'deliveries.txt'

Save

Great! The exception handler displays an error message with an icon that really alerts the user to the problem. Let's check back with Head-Ex to see if they like it.

durian delivered

That's exactly what we need. And not a moment too soon. We just had a delivery of durian, the world's stinkiest fruit. There's no way we want to leave THAT delivery too long in the stock room!

The error message box was exactly what Head-Ex needed.

By catching exceptions and displaying important information in message boxes, you can greatly improve the experience of your users when things go wrong.

Great work!

You've got Chapter 8.5 under your belt. Let's look back at what you've learned in this chapter:

Your Programming Toolbox

You've got Chapter 8.5 under your belt. Let's look back at what you've learned in this chapter:

9 graphical interface elements

Selecting the right tool

It's easy to make your programs more effective for your users.

And when it comes to GUI applications, there's a world of difference between a working interface and one that's both useful and effective. Selecting the right tool for the right job is a skill that comes with experience, and the best way to get that experience is to use the tools available to you. In this chapter, you'll continue to expand your GUI application building skills. There's a bunch of truly useful widgets waiting to be experienced. So, turn the page and let's get going.

dj goes digital

Was this article helpful?

0 0

Post a comment