2014年6月1日日曜日

開発環境

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)、POOL PUZZLE, SHARPEN YOUR PENCIL(p.279)を解いてみる。

POOL PUZZLE, SHARPEN YOUR PENCIL(p.279)

コード(BBEdit)

sample279.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)))
        # SHARPEN, YOUR PENDIL
        depot.set(None)
        description.delete(0, tkinter.END)
        address.delete('1.0', tkinter.END)

app = tkinter.Tk()
app.title('Head-Ex Delivers')

tkinter.Label(app, text='Deposit:').pack()
depot = tkinter.StringVar()
depot.set(None)
tkinter.Radiobutton(
    app, text='Cambridge, MA', value='Cambridge, MA', variable=depot).pack()
tkinter.Radiobutton(
    app, text='Cambridge, UK', value='Cambridge, UK', variable=depot).pack()
tkinter.Radiobutton(
    app, text='Seattle, WA', value='Seattle, WA', variable=depot).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)

$ ./sample279.py
$ cat deliveries.txt 
Depot:
Cambridge, MA
Description:
Book1
Address:
1 Main Street
Anytown1

Depot:
Cambridge, UK
Description:
Books2
Address:
2 Main Street
Anytown2

$

0 コメント:

コメントを投稿