Page 405 -
P. 405
class replaces function
The SoundPanel class looks a lot like the
create gui() function
If you convert the original change_volume() function to a method and
add it to the class, you end up with code that looks rather like the original
create_gui() function:
from tkinter import *
import pygame.mixer
class SoundPanel(Frame):
def track_toggle(self):
Most of this code looks if self.track_playing.get() == 1:
very similar to the
“create_gui()” method, self.track.play(loops = -1)
else:
except for all those uses
of “self”. self.track.stop()
def change_volume(self):
self.track.set_volume(self.volume.get())
In fact, the new SoundPanel() class can completely replace the code in
the sound_panel.py file (as create_gui() is no longer needed).
But before you do that, there’s still a little more code to write. The class needs
to be told what to do when the brand new SoundPanel() is created. The
class needs an initializer method that knows how to create instances of the
class.
Some programming languages call these
initializer methods CONSTRUCTORs, because
they detail what happens when a new object
is created or “constructed.”
Let’s create the initializer for the SoundPanel() class.
370 Chapter 10

