2. Class = object factory

2.1. Class SoundPanel

2.2. Main program

  • Try running this code in your PC and see the code dynamically generate soundpanels for wav files in your directory.

from tkinter import *
from sound_panel import *
import pygame.mixer
import os

app = Tk()
app.title("Head First Mix")

mixer = pygame.mixer
mixer.init()

dirList = os.listdir(".")
for fname in dirList:
   if fname.endswith(".wav") and fname[0] in '345':
      SoundPanel(app,mixer,fname).pack()

def shutdown():
   mixer.stop()
   app.destroy()

app.protocol("WM_DELETE_WINDOW",shutdown)
app.mainloop()
  • Congratulations! Now you know how to create complex programs with little code.

2.3. Remember

  • Classes are object factories

  • Class = methods + data

  • Create objects = instantiate a class

  • class - begins the definition of a class

  • self - assigned to the current object

  • __init__() - called when the object is created

You have attempted of activities on this page