Page 359 -
P. 359

shutdown function




                                                Now that you know about window manager properties and
                                                how to capture them, you were asked to write the code for the
                                                shutdown() function:


                                   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)


                                    def track_stop():
                                        track.stop()
                                    def shutdown():
                 Simply arrange for the     track.stop()
                 track to stop playing
                 when the window closes.




                                   track = mixer.Sound(sound_file)


                                   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)
               Call “app.protocol()”   app.protocol(“WM_DELETE_WINDOW", shutdown)
               before the call to
              “app.mainloop()”.     app.mainloop()




           324    Chapter 9
   354   355   356   357   358   359   360   361   362   363   364