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 8(Guis and Data: Data entry widgets)、LONG EXERCISE(p.286)を解いてみる。
LONG EXERCISE(p.286)
コード(BBEdit)
sample286.py
#!/usr/bin/env python3 #-*- coding: utf-8 import tkinter def saveData(): with open('deliveries.txt', 'a') as f: f.write('Depot:\n') f.write('{0}\n'.format(depot.get())) f.write('Description:\n') f.write('{0}\n'.format(description.get())) f.write('Address:\n') f.write('{0}\n'.format(address.get('1.0', tkinter.END))) depot.set(None) description.delete(0, tkinter.END) address.delete('1.0', tkinter.END) 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)
$ ./sample286.py $ cat deliveries.txt Depot: Cambridge, MA Description: Books1 Address: Street1 Town1 Depot: Edinburgh, Scotland Description: Books2 Address: Street2 Town2 City2 State2 $
0 コメント:
コメントを投稿