PlaySound () slows down the process

My program has the following code:

self["relief"] = SUNKEN
self.banged = True
self.lost = True
self.lettersLeft = 0
self.lettersBanged = self.lettB
winsound.PlaySound('sound.wav', winsound.SND_FILENAME)
messagebox.showerror("Letter Banging","Sorry, you lost the game!", parent=self)
for key in self.squares.keys():
    if self.squares[key].value == 3:
        self.squares[key].banged = False


I just added a part winsound.PlaySound('sound.wav', winsound.SND_FILENAME)

and it slowed down my program. Infact, it plays the sound first and then does what is in front of it. I am using Python with tKinter. Any suggestions?


source to share

1 answer

When you change a widget property such as editing content, background and relief, this change does not appear immediately, they are recorded and only take effect when you give a hand to the mainloop that causes your application to redraw. This leads to the behavior you observe: the sound plays, then the callback completes and the changes are redrawn.

All the time you spend on the callback playing audio, your application will not be responsive. If you appreciate that your audio is short enough, you can add self.update()

somewhere between the UI change you want to show first and the call to PlaySound.

If you want to avoid any unresponsiveness in your application, you can play the audio on a different stream

sound_thread = threading.Thread(target=lambda:winsound.PlaySound('sound.wav', winsound.SND_FILENAME))




All Articles