Page 387 -
P. 387
new function
Here is the code from the end of the previous chapter. Study it carefully, and then, in the
space on the next page, write the code for your new function (based on the code below).
Begin by importing the
from tkinter import *
import pygame.mixer libraries you need.
Create the GUI application...
app = Tk()
app.title("Head First Mix")
sound_file = "50459_M_RED_Nephlimizer.wav"
mixer = pygame.mixer
...and initialize the
mixer.init() sound system.
def track_toggle():
if track_playing.get() == 1:
track.play(loops = -1) The event-handler functions
else: detail what happens when an
track.stop() event occurs.
def change_volume(v):
Define the checkbox widget.
track.set_volume(volume.get())
track = mixer.Sound(sound_file)
track_playing = IntVar()
track_button = Checkbutton(app, variable = track_playing,
command = track_toggle, text = sound_file)
track_button.pack(side = LEFT)
Define the slider widget.
volume = DoubleVar()
volume.set(track.get_volume())
volume_scale = Scale(variable = volume, from_ = 0.0, to = 1.0, resolution = 0.1,
command = change_volume, label = "Volume", orient = HORIZONTAL)
volume_scale.pack(side = RIGHT)
def shutdown():
Handle a click on
track.stop() the close box.
app.destroy()
app.protocol("WM_DELETE_WINDOW", shutdown)
Start the event loop.
app.mainloop()
352 Chapter 10