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 11(Custom Widgets and Classes: With an object in mind)、CODE MAGNETS(p.371)を解いてみる。
CODE MAGNETS(p.371)
コード(BBEdit)
sample371.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
class SoundPanel(Frame):
def __init__(self, app, mixer, sound_file):
Frame.__init__(self, app)
self.track = mixer.Sound(sound_file)
self.track_playing = IntVar()
track_button = Checkbutton(self,
variable=self.track_playing,
command=self.trackToggle,
text=sound_file)
track_button.pack(side=LEFT)
self.volume = DoubleVar()
volume_scale = Scale(self,
variable = self.volume,
from_ = 0.0,
to = 1.0,
resolution = 0.1,
command = self.changeVolume,
label = 'Volume',
orient = HORIZONTAL)
volume_scale.pack(side=RIGHT)
def trackToggle(self):
if self.track_playing.get() == 1:
self.track.play(loops=-1)
else:
self.track.stop()
def changeVolume(self, a):
self.track.set_volume(self.volume.get())
test_sample371.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
import sample371
def shutdown():
mixer.stop()
app.destroy()
app = Tk()
app.title('Head First Mix')
mixer = pygame.mixer
mixer.init()
sound_files = ['39147_M_RED_beep_line.wav',
'39484__M_RED__trance_trumpet_loop.wav',
'39607__M_RED__trumpet_delay_loop.wav',
'41722__M_RED__happy_freaq.wav',
'45414_M_RED_Trance_Train.wav',
'49119_M_RED_HardBouncer.wav',
'50459_M_RED_Nephlimizer.wav',
'50848_M_RED_WaveBaseLoop.wav',
'59264_M_RED_TheDreamDrums.wav']
for sound_file in sound_files:
panel = sample371.SoundPanel(app, mixer, sound_file)
panel.pack()
app.protocol('WM_DELETE_WINDOW', shutdown)
app.mainloop()
入出力結果(Terminal)
$ ./test_sample371.p $
0 コメント:
コメントを投稿