Problem with While and For in wxPython

Hi, thanks Robin for your answer !!!
And I'm going to try to be clearer.
So, I would like to make a simple media Player with
wxPython, in a wxPanel with four buttons:
Play, Stop, Back, and Next.
And when I press the Play button, I would like to hear
the list of all my songs, one after other.
So, here is a part of my sample code:

···

#----------------------------------------
from wxPython.wx import *
import time
import FmodLibrary # a library to play all files midi
and audio

# ids of buttons
ID_PLAY =2
ID_STOP =4
ID_BACK =6
ID_NEXT =8

# list of songs
list_song = ['song01.wav', 'song02.wav', 'song03.wav',
'song04.wav', 'song05.wav']

class MediaPanel(wxPanel):
    """ class for buttons """
     def __init__(self, parent, id):
         wxPanel.__init__(self, parent, id,
style=wxTAB_TRAVERSAL)

         self.play = wxButton(self, ID_PLAY, "Play",
(25, 80))
         self.stop = wxButton(self, ID_STOP, "Stop",
(25, 140))
         self.back = wxButton(self, ID_BACK, "Back",
(25, 200))
         self.next = wxButton(self, ID_NEXT, "Next",
(25, 260))

         EVT_BUTTON(self, self.play.GetId(),
self.OnPlay)
         EVT_BUTTON(self, self.stop.GetId(),
self.OnStop)
         EVT_BUTTON(self, self.back.GetId(),
self.OnBack)
         EVT_BUTTON(self, self.next.GetId(),
self.OnNext)

        def OnPlay(self, evt):
            # Attention, problems starting at here:
            for index in range(len(list_song)): # OK
                self.song = list_song[index] # OK

                # FmodPlayer is a class which use Fmod
Library to play all formats of audio and midi files,
                # not implemented here to be not too
long
                evt = FmodPlayer() # call the class:
OK
                evt.Load_It(self.song) # load the
song: OK
                evt.Play_It() # play the song: OK

                # problem here:
                while (evt.IsPlaying): # function to
tell if the song is playing:
                    time.sleep(1) # ??? Because I
can't press the Stop button if I wish it

        def OnStop(self, evt):
            # FmodPlayer is a class which use Fmod
Library to play all formats of audio and midi files,
            # not implemented here to be not too long
            evt = FmodPlayer() # call the class: OK
            evt.Stop_It() # stop the song: ???

#----------------------------------------

The problem is when the program is in the while loop,
I can't press any button, or use the mouse.
So, I can't press the Stop, Back or Next button.
I must wait the end of the last song in the list to
access to buttons.
How can I do?

Big thanks !!!

SAMMM

___________________________________________________________
Do You Yahoo!? -- Une adresse @yahoo.fr gratuite et en français !
Yahoo! Mail : http://fr.mail.yahoo.com

You don’t need the while loop at all. What you need is a thread to play
the music in. A wxPython app should just sit there waiting for events
to occur and then responding, you are tying up the "listener" for these
events with this while loop. You need to look into using threads to do
this, check out the demo and the python docs.

Sam

Heres an idea of what it should look like
  #In play button
  Thread(target = self.playsong, args = {filename}).start()

def playsong(self, file):
  #start song
  play(file)
  #wait for it to end...
  while(isPlaying):
    sleep(1)

···

-----Original Message-----
From: SAMMM [mailto:immortel_sam@yahoo.fr]
Sent: Saturday, May 24, 2003 2:49 PM
To: wxpython-users@lists.wxwindows.org
Subject: [wxPython-users] Problem with While and For in wxPython

Hi, thanks Robin for your answer !!!
And I'm going to try to be clearer.
So, I would like to make a simple media Player with
wxPython, in a wxPanel with four buttons:
Play, Stop, Back, and Next.
And when I press the Play button, I would like to hear
the list of all my songs, one after other.
So, here is a part of my sample code:

#----------------------------------------
from wxPython.wx import *
import time
import FmodLibrary # a library to play all files midi
and audio

# ids of buttons
ID_PLAY =2
ID_STOP =4
ID_BACK =6
ID_NEXT =8

# list of songs
list_song = ['song01.wav', 'song02.wav', 'song03.wav', 'song04.wav',
'song05.wav']

class MediaPanel(wxPanel):
    """ class for buttons """
     def __init__(self, parent, id):
         wxPanel.__init__(self, parent, id,
style=wxTAB_TRAVERSAL)

         self.play = wxButton(self, ID_PLAY, "Play",
(25, 80))
         self.stop = wxButton(self, ID_STOP, "Stop",
(25, 140))
         self.back = wxButton(self, ID_BACK, "Back",
(25, 200))
         self.next = wxButton(self, ID_NEXT, "Next",
(25, 260))

         EVT_BUTTON(self, self.play.GetId(),
self.OnPlay)
         EVT_BUTTON(self, self.stop.GetId(),
self.OnStop)
         EVT_BUTTON(self, self.back.GetId(),
self.OnBack)
         EVT_BUTTON(self, self.next.GetId(),
self.OnNext)

        def OnPlay(self, evt):
            # Attention, problems starting at here:
            for index in range(len(list_song)): # OK
                self.song = list_song[index] # OK

                # FmodPlayer is a class which use Fmod
Library to play all formats of audio and midi files,
                # not implemented here to be not too
long
                evt = FmodPlayer() # call the class:
OK
                evt.Load_It(self.song) # load the
song: OK
                evt.Play_It() # play the song: OK

                # problem here:
                while (evt.IsPlaying): # function to
tell if the song is playing:
                    time.sleep(1) # ??? Because I
can't press the Stop button if I wish it

        def OnStop(self, evt):
            # FmodPlayer is a class which use Fmod
Library to play all formats of audio and midi files,
            # not implemented here to be not too long
            evt = FmodPlayer() # call the class: OK
            evt.Stop_It() # stop the song: ???

#----------------------------------------

The problem is when the program is in the while loop,
I can't press any button, or use the mouse.
So, I can't press the Stop, Back or Next button.
I must wait the end of the last song in the list to
access to buttons.
How can I do?

Big thanks !!!

SAMMM

___________________________________________________________
Do You Yahoo!? -- Une adresse @yahoo.fr gratuite et en français ! Yahoo!
Mail : http://fr.mail.yahoo.com

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwindows.org
For additional commands, e-mail: wxPython-users-help@lists.wxwindows.org