Page 369 -
P. 369
checkbox added
1 Here’s your code from earlier. You were to use your pencil to put a
line through the code you don’t need anymore:
from tkinter import *
import pygame.mixer
app = Tk()
app.title("Head First Mix")
app.geometry('250x100+200+100')
sound_file = "50459_M_RED_Nephlimizer.wav"
mixer = pygame.mixer
mixer.init()
def track_start():
track.play(loops = -1)
The functions that start and stop
def track_stop(): the track are no longer needed.
track.stop()
def shutdown():
track.stop()
app.destroy()
You can also get rid of the two buttons,
track = mixer.Sound(sound_file) since they aren't needed either.
start_button = Button(app, command = track_start, text = "Start")
start_button.pack(side = LEFT)
stop_button = Button(app, command = track_stop, text = "Stop")
stop_button.pack(side = RIGHT)
app.protocol("WM_DELETE_WINDOW", shutdown)
app.mainloop()
Write the code you need to implement the checkbox here. You were asked to give your IntVar the name
2
track_playing, use track_toggle as your function name, and call the checkbox track_button:
def track_toggle():
The “track_toggle" function if track_playing.get() == 1:
either plays or stops the All of this code needs to
track, based on the state of track.play(loops = -1) be added to your program
the checkbox. else: BEFORE the call to “app.
track.stop() mainloop()".
track_playing = IntVar()
track_button = Checkbutton(app, variable = track_playing,
Use the name of the sound command = track_toggle,
file as the text associated text = sound_file)
with the checkbox.
track_button.pack()
334 Chapter 9