2014年11月19日水曜日

開発環境

Practical Programming: An Introduction to Computer Science Using Python 3 (Pragmatic Programmers) (Paul Gries (著)、Jennifer Campbell (著)、Jason Montojo (著)、Lynn Beighley (編集)、Pragmatic Bookshelf)のChapter 16(Creating Graphical User Interfaces)、16.7(Exercises) 4.を解いてみる。

16.7(Exercises) 4.

コード(BBEdit)

sample4.py

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
 
import tkinter

def count(text):
    a = 0
    t = 0
    c = 0
    g = 0
    for ch in text.get('0.0', tkinter.END):
        if ch == 'A':
            a += 1
        elif ch == 'T':
            t += 1
        elif ch == 'C':
            c += 1
        elif ch == 'G':
            g += 1
    dna.set('Num As:{0} Num Ts:{1} Num Cs:{2} Num Gs:{3}'.format(a, t, c, g))

app = tkinter.Tk()

frame = tkinter.Frame(app)
frame.pack()

text = tkinter.Text(frame, width=50, height=10)
text.pack()

dna = tkinter.StringVar()
button = tkinter.Button(frame, text='Count', command=lambda: count(text))
button.pack()

label = tkinter.Label(frame, textvariable=dna)
label.pack()

app.mainloop()

入出力結果(Terminal, IPython)

$ ./sample4.py
$

0 コメント:

コメントを投稿