
308 第 8 ½ 章
有消息嗎?
Head-Ex 的人想要你的程式在儲存紀錄到 deliveries.txt 檔案
有問題時展示這道訊訊︰
完成建立訊息方框的這段程式碼當中闕漏的那幾行。
提示︰你必須在訊息方框的文字裡包含換行符號。
from tkinter import *
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)
address.delete("1.0", END)
except Exception as ex:
import tkinter.messagebox
tkinter.messagebox.showerror(“Error!", “Can't write to the file\n %s" % ex)
你應該使用 showerror() 函式,讓對話框
具有正確的錯誤圖示(icon)。
記得匯入必
要的模組。
ch08.5.indd 308 2015/8/17 ...