Page 410 -
P. 410

custom widgets and classes






           sound_panel.py



            from tkinter import *           An initializer method comes first. Note that this method has to be
                                            called “__init__()” in Python in order to be called automatically
            import pygame.mixer
                                            when the object is created.
            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.track_toggle, text = sound_file)
                    track_button.pack(side = LEFT)
                    self.volume = DoubleVar()
                    self.volume.set(self.track.get_volume())
                    volume_scale = Scale(self, variable = self.volume, from_ = 0.0, to = 1.0,
                                            resolution = 0.1, command = self.change_volume,
                                               label = "Volume", orient = HORIZONTAL)
                    volume_scale.pack(side = RIGHT)

                def track_toggle(self):
                    if self.track_playing.get() == 1:
                        self.track.play(loops = -1)
                    else:
                        self.track.stop()

                def change_volume(self, v):
                    self.track.set_volume(self.volume.get())














                                                                                      you are here 4    375
   405   406   407   408   409   410   411   412   413   414   415