Head First Programming
A learner's guide to programming
using the Python language
( O'Reilly Media)
David Griffiths (著) Paul Barry (著)
開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Python (プログラミング言語)
Head First Programming A learner's guide to programming using the Python language (David Griffiths(著)、Paul Barry(著)、 O'Reilly Media )のChapter 9(Exceptions and Message Boxes: Get the message?)、SHARPEN YOUR PENCIL(p.307)を解いてみる。
SHARPEN YOUR PENCIL(p.307)
コード(BBEdit)
sample307.py
#!/usr/bin/env python3 #-*- coding: utf-8 import tkinter import tkinter.messagebox def saveData(): try: file_data = open('deliveries.txt', 'a') file_data.write('Depot:\n') file_data.write('{0}\n'.format(depot.get())) file_data.write('Description:\n') file_data.write('{0}\n'.format(description.get())) file_data.write('Address:\n') file_data.write('{0}\n'.format(address.get('1.0', tkinter.END))) depot.set(None) description.delete(0, tkinter.END) address.delete('1.0', tkinter.END) except Exception as ex: tkinter.messagebox.showerror('Error!', "Can't write to the file {0}".format(ex)) def readDepots(file): with open(file) as f: depots = [depot.rstrip() for depot in f] return depots app = tkinter.Tk() app.title('Head-Ex Delivers') tkinter.Label(app, text='Deposit:').pack() depot = tkinter.StringVar() depot.set(None) tkinter.OptionMenu(app, depot, *readDepots('depots.txt')).pack() tkinter.Label(app, text='Description:').pack() description = tkinter.Entry(app) description.pack() tkinter.Label(app, text='Address:').pack() address = tkinter.Text(app) address.pack() tkinter.Button(app, text='Save', command=saveData).pack() app.mainloop()
入出力結果(Terminal)
$ ls -l deliveries.txt -r--r--r-- 1 kamimura staff 149 Jun 2 13:38 deliveries.txt $ ./sample307.py $
0 コメント:
コメントを投稿