Head First Programming - hlmix.py(final)

 1 from tkinter import *
 2 import pygame.mixer
 3 class SoundPanel(Frame):
 4     def __init__(self, app, mixer, sound_file):
 5         Frame.__init__(self, app)
 6         self.track = mixer.Sound(sound_file)
 7         self.track_playing = IntVar()
 8         track_button = Checkbutton(self, variable = self.track_playing,
 9                                    command = self.track_toggle, text = sound_file)
10         track_button.pack(side = LEFT)
11         self.volume = DoubleVar()
12         self.volume.set(self.track.get_volume())
13         volume_scale = Scale(self,variable = self.volume,
14                              from_ = 0.0,
15                              to = 1.0,
16                              resolution = 0.1,
17                              command = self.change_volume,
18                              label = "Volume",
19                              orient = HORIZONTAL)
20         volume_scale.pack(side = RIGHT)
21     def track_toggle(self):
22         if self.track_playing.get() == 1:
23             self.track.play(loops = -1)
24         else:
25             self.track.stop()
26     def change_volume(self, v):
27         self.track.set_volume(self.volume.get())
28     
29     
30     
31   
 1 from tkinter import *
 2 import pygame.mixer
 3 class SoundPanel(Frame):
 4     def __init__(self, app, mixer, sound_file):
 5         Frame.__init__(self, app)
 6         self.track = mixer.Sound(sound_file)
 7         self.track_playing = IntVar()
 8         track_button = Checkbutton(self, variable = self.track_playing,
 9                                    command = self.track_toggle, text = sound_file)
10         track_button.pack(side = LEFT)
11         self.volume = DoubleVar()
12         self.volume.set(self.track.get_volume())
13         volume_scale = Scale(self,variable = self.volume,
14                              from_ = 0.0,
15                              to = 1.0,
16                              resolution = 0.1,
17                              command = self.change_volume,
18                              label = "Volume",
19                              orient = HORIZONTAL)
20         volume_scale.pack(side = RIGHT)
21     def track_toggle(self):
22         if self.track_playing.get() == 1:
23             self.track.play(loops = -1)
24         else:
25             self.track.stop()
26     def change_volume(self, v):
27         self.track.set_volume(self.volume.get())
28     
29     
30     
31   

 

posted on 2013-01-07 20:04  mybluecode  阅读(127)  评论(0编辑  收藏  举报