2020年7月30日木曜日

開発環境

Practical Programming: An Introduction to Computer Science Using Python 3.6 (Paul Gries(著)、Jennifer Campbell(著)、Jason Montojo(著)、Pragmatic Bookshelf)のChapter 16(Creating Graphical User Interfaces)、Exercise 4.の解答を求めてみる。

コード

#!/usr/bin/env python3
import tkinter


class DNA:
    def __init__(self, parent):
        self.parent = parent
        # Model
        self.nums = tkinter.StringVar()
        # View
        self.text = tkinter.Text(self.parent)
        self.text.pack()
        self.button = tkinter.Button(self.parent,
                                     text='Count',
                                     command=self.count)
        self.button.pack()
        self.label = tkinter.Label(self.parent, textvariable=self.nums)
        self.label.pack()

    # Controller
    def count(self):
        atcg = {}
        for ch in self.text.get(0.0, tkinter.END):
            atcg.setdefault(ch, 0)
            atcg[ch] += 1
        self.nums.set(
            f'Num As: {atcg.get("A", 0)} Num Ts: {atcg.get("T", 0)} ' +
            f'Num Cs: {atcg.get("C", 0)} Num Gs: {atcg.get("G", 0)}')


if __name__ == "__main__":
    app = tkinter.Tk()
    counter = DNA(app)
    app.mainloop()

入出力結果(Zsh、PowerShell、Terminal、Jupyter(IPython))

% ./sample4.py
%

0 コメント:

コメントを投稿