Page 343 -
P. 343
get the message
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:
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
import the
necessary 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:
tkinter.messagebox.showerror(“Error!", “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.
308 Chapter 8 ½