[Tkinter-discuss] Button Press Freezes

Michael Lange klappnase at web.de
Wed May 15 17:49:56 CEST 2013


Hi,

On Wed, 15 May 2013 15:31:32 +0530
Quaki Gaffar <quakig at live.com> wrote:

> Here's my situation. I have two tkinter buttons:
> 
>     Play Button: plays a sound
>     Stop Button: to stop the sound during play
> 
> Code is as follows:
> 
> def Play(self):
>     //plays a file with pygame module
> 
> def Stop(self):
>     //Stop using pygame stop
> 
> 
> Button( root, text ='Play', command= Play).pack()
> Button( root, text ='Stop', command= Stop).pack()
> 
> The issue is that, when I click the 'Play' button, it stays pressed
> until it completes playing the sound, freezing the entire program, not
> allowing me to press 'stop' button during that time.
> 
> How do i prevent the program from freezing while the play is in
> progress to allow me to do other things like stop?

I never used pygame myself, so I can only guess that the pygame method
you use (pygame.mixer.music.play()?) does not return until the playback is
done.
If this is the case, you will have to use a background thread to
do the playback. This is not necessarily overly complicated and perfectly
safe as long as you make sure that the tk gui runs in the main program
thread and that you must never do any calls to tk methods from within any
of the child threads; see here:
http://code.activestate.com/lists/python-tkinter-discuss/3138/
for a (however quite primitive and useless) example of a basic technique
for setting up threaded Tkinter programs.

Otoh, if you only need some simple audio playback and not necessarily
require pygame, there might be other python libs available that can
handle this, for example iirc tkSnack (http://www.speech.kth.se/snack/)
supports non-blocking playback (though tkSnack today is rather dated,
but probably still works with Python2.x). There are also PyAudio and
Pyglet and maybe others, however I never used any of them myself.

Regards

Michael



.-.. .. ...- .   .-.. --- -. --.   .- -. -..   .--. .-. --- ... .--. . .-.

Without freedom of choice there is no creativity.
		-- Kirk, "The return of the Archons", stardate 3157.4


More information about the Tkinter-discuss mailing list