Python Programming for Raspberry Pi, Sams Teach Yourself in 24 Hours

(singke) #1
if Event.type in (QUIT,KEYDOWN,MOUSEBUTTONDOWN):
Graceful_Exit()

But wait! This actually doesn’t work! For PyGame to properly handle events, the display screen must
be initialized. Thus, you need to set up a simple display screen to gracefully control the end of your
script. Here’s an example:


Click here to view code image


MusicScreen = display.set_mode((0,0))
display.set_caption("Playing Music...")

Now, when your music plays, a screen pops up, with the caption "Playing Music" at the top.
You can minimize that screen and listen to your music playlist. When you are done, you just maximize
the screen and either click it with your mouse or press any key on the keyboard to stop the music.


Since the display screen is already initialized in your music script, you might think that you could add
images to be displayed on the screen while the music plays—and that’s a good idea! You’ll learn
how to do that a bit later this hour, but first you need to do a few more things related to your music
script, including reviewing the music script in its entirety. See Listing 23.4.


LISTING 23.4 The script2303.py Music Script


Click here to view code image


#script2303.py - Play Music from List
#Written by Blum and Bresnahan
#
##########################################
#
##### Import Functions & Variables #######
#
from os import system #Import from OS module
#
#Import from PyGame Library
from pygame import display, event, init, mixer, time
#
from sys import exit #Import from System module
#
from pygame.locals import * #Load PyGame constants
#
init() #Initialize PyGame
#
# Load Music Play List Function ##################
#
# Read Playlist and Queue up Songs #
#
def Load_Music ():
#
global SongList #Make SongList global
SongList = [] #Initialize SongList to Null
#
global SongNumber #Make SongNumber global
SongNumber = 0 #Initialize Song Number to 0
#
global MaxSongs #Make MaxSongs global
MaxSongs = 0 #Initialize Maximum Songs to 0
#
Free download pdf