2014年5月29日木曜日

開発環境

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

SHARPEN YOUR PENCIL(p.240)

コード(BBEdit)

sample240.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

# 必要な関数 playCorrectSound, playWrongSound

# 必要な箇所を残したり加えたり
sounds = pygame.mixer
sounds.init()
currect_s = sounds.Sound('correct.wav')
wrong_s = sounds.Sound('wrong.wav')

number_correct = 0
number_wrong = 0

def waitFinish(channel):
    while channel.get_busy():
        pass

def playCorrectSound():
    global number_correct
    number_correct += 1
    waitFinish(currect_s.play())
    
def playWrongSound():
    global number_wrong
    number_wrong += 1
    waitFinish(wrong_s.play())

app = Tkinter.Tk()
app.title('TVN')
app.geometry('280x100+200+100')

correct_button = Tkinter.Button(
    app, text='Correct Answer', width=10, command=playCorrectSound)
correct_button.pack(side='left', padx=10, pady=10)

wrong_button = Tkinter.Button(
    app, text='Wrong Answer', width=10,command=playWrongSound)
wrong_button.pack(side='right', padx=10, pady=10)

app.mainloop()

print('{0} were correctly answered.'.format(number_correct))
print('{0} were answered incorrectly.'.format(number_wrong))

入出力結果(Terminal)

$ ./sample240.py 
4 were correctly answered.
2 were answered incorrectly.
$

0 コメント:

コメントを投稿