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 7(Building a Graphical User Interface: Going all gooey)、LONG EXERCISE(p.250)を解いてみる。
LONG EXERCISE(p.250)
コード(BBEdit)
sample250.py
#!/usr/bin/env python2.7
#-*- coding: utf-8
# python3.xのpygameが上手くインストールできてないので、とりあえず2.7で作成
# python3.xのtkinterモジュールは、2.7ではTkinterであることに注意
import pygame.mixer
import Tkinter
sounds = pygame.mixer
sounds.init()
currect_s = sounds.Sound('correct.wav')
wrong_s = sounds.Sound('wrong.wav')
def waitFinish(channel):
while channel.get_busy():
pass
def playCorrectSound():
number_correct.set(number_correct.get() + 1)
waitFinish(currect_s.play())
def playWrongSound():
number_wrong.set(number_wrong.get() + 1)
waitFinish(wrong_s.play())
app = Tkinter.Tk()
app.title('TVN')
app.geometry('400x100+200+100')
top_label = Tkinter.Label(
app, text='When you are ready, click on the button!', height=3)
top_label.pack(side='top')
number_correct = Tkinter.IntVar()
number_correct.set(0)
correct_label = Tkinter.Label(app, textvariable=number_correct)
correct_label.pack(side='left')
number_wrong = Tkinter.IntVar()
number_wrong.set(0)
wrong_label = Tkinter.Label(app, textvariable=number_wrong)
wrong_label.pack(side='right')
correct_button = Tkinter.Button(
app, text='Correct', width=15, command=playCorrectSound)
correct_button.pack(side='left', padx=10, pady=10)
wrong_button = Tkinter.Button(
app, text='Wrong', width=15,command=playWrongSound)
wrong_button.pack(side='right', padx=10, pady=10)
app.mainloop()
入出力結果(Terminal)
$ ./sample250.py $
0 コメント:
コメントを投稿