2014年6月11日水曜日

開発環境

Head First Programming A learner's guide to programming using the Python language (David Griffiths(著)、Paul Barry(著)、 O'Reilly Media )のChapter 11(Custom Widgets and Classes: With an object in mind)、LONG EXERCISE(p.352)を解いてみる。

LONG EXERCISE(p.352)

コード(BBEdit)

sample342.py

#!/usr/bin/env python2.7
#-*- coding: utf-8 -*-

# python3.xではtkinter, python2.xではTkinter
# python3.xでpygame.mixerモジュールを上手くインストールできてないので2.7を使用
from Tkinter import *

import pygame.mixer

def createGuiApp(app, title, sound_file):
    def trackToggle():
        if track_playing.get() == 1:
            track.play(loops=-1)
        else:
            track.stop()
    def shutdown():
        track.stop()
        app.destroy()

    def changeVolume(a):
        track.set_volume(volume.get())

    app.title(title)

    mixer = pygame.mixer
    mixer.init()
    track = mixer.Sound(sound_file)

    track_playing = IntVar()
    track_button = Checkbutton(app,
                               variable=track_playing,
                               command=trackToggle,
                               text=sound_file)
    track_button.pack(side=LEFT)

    volume = DoubleVar()
    volume_scale = Scale(app,
                         variable = volume,
                         from_ = 0.0,
                         to = 1.0,
                         resolution = 0.1,
                         command = changeVolume,
                         label = 'Volume',
                         orient = HORIZONTAL)
    volume_scale.pack(side=RIGHT)

    app.protocol('WM_DELETE_WINDOW', shutdown)

app = Tk()

sound_file = '50459_M_RED_Nephlimizer.wav'

createGuiApp(app, 'Head First Mix', sound_file)

app.mainloop()

入出力結果(Terminal)

$ ./sample352.py
$

0 コメント:

コメントを投稿