Page 350 -
P. 350

graphical interface elements


                       Code Magnets

                       A couple of buttons on a GUI ought to do it. Here’s the code to a
                       small tkinter program that starts and stops a sound file. Rearrange
                       the code magnets to make the program:





































            from tkinter import *
            import pygame.mixer
                                                                              def track_start():
                        track = mixer.Sound(sound_file)                          track.play(loops = -1)

                  stop_button = Button(app, command = track_stop, text = "Stop")
                  stop_button.pack(side = RIGHT)



            app.mainloop()         start_button.pack(side = LEFT)
                                   start_button = Button(app, command = track_start, text = "Start")


                                          mixer = pygame.mixer
               def track_stop():                                       app = Tk()
                                          mixer.init()
                  track.stop()                                         app.title("Head First Mix")
                                                                       app.geometry('250x100+200+100')
                  sound_file = "50459_M_RED_Nephlimizer.wav"
                                                                                       you are here 4    315
   345   346   347   348   349   350   351   352   353   354   355