開発環境
- OS X Lion - Apple(OS)
- BBEdit - Bare Bones Software, Inc., Emacs(Text Editor)
- プログラミング言語: Python
初めてのコンピュータサイエンス(Jennifer Campbell、Paul Gries、Jason Montojo、Greg Wilson(著)長尾 高弘(翻訳))の14章(GUI)の14.8(練習問題)を解いてみる。
1.
コード(BBEdit)
sample.py
#!/usr/bin/env python3.3 #-*- coding: utf-8 -*- import tkinter window = tkinter.Tk() label = tkinter.Label(window, text="Good-bye") label.pack() button = tkinter.Button(window, text="close", command=lambda: window.destroy()) button.pack() window.mainloop()
2.
コード(BBEdit)
sample.py
#!/usr/bin/env python3.3 #-*- coding: utf-8 -*- import tkinter def click(var, value): var.set(var.get() + value) if __name__ == '__main__': window = tkinter.Tk() counter = tkinter.IntVar() counter.set(0) button = tkinter.Button(window, textvariable=counter, command=lambda: click(counter, 1)) button.pack() window.mainloop()
4.
コード(BBEdit)
sample.py
#!/usr/bin/env python3.3 #-*- coding: utf-8 -*- import tkinter def counter(dna, count): atgc = {'A':0, 'T':0, 'G':0, 'C':0} text = dna.get('0.0', 'end') for c in text: if c in atgc: atgc[c] += 1 count.set("Aの数: {0} Tの数: {1} Cの数: {2} Gの数: {3}".format( atgc['A'], atgc['T'], atgc['C'], atgc['G'])) window = tkinter.Tk() frame = tkinter.Frame(window) frame.pack() dna = tkinter.Text(frame, height=20, width=40, borderwidth=4, relief='solid') dna.pack() button = tkinter.Button(frame, text="数える", command=lambda:counter(dna, count)) button.pack() count = tkinter.StringVar() result = tkinter.Label(frame, textvariable=count) result.pack() window.mainloop()
5.
コード(BBEdit)
sample.py
#!/usr/bin/env python3.3 #-*- coding: utf-8 -*- import tkinter window = tkinter.Tk() frame = tkinter.Frame(window) frame.pack() title = tkinter.Label(frame, text="華氏表現の温度") title.pack() fahrenheit = tkinter.Entry(frame) fahrenheit.pack() celsius = tkinter.StringVar() celsius.set("") result = tkinter.Label(frame, textvariable=celsius) result.pack() convert = tkinter.Button(frame, text="変換", command=lambda: celsius.set(str(5 / 9 * (float(fahrenheit.get()) - 32)))) convert.pack() close = tkinter.Button(frame, text="終了", command=lambda: window.destroy()) close.pack() window.mainloop()
0 コメント:
コメントを投稿