0

When I use tkinter with playsound it frezes untill the sound is played. I have tried using block = False but then it dosnt play the sound at all!

This is part of my code:

from tkinter import *
from tkinter import ttk
from playsound import playsound


class GameMaker:
    def __init__(self,root=None):
        self.sounds={}
    def AddSound(self,name,directory,overide=False,times=0,TimesOveride=0):
        if times != 0:
            name = name + str(times)
        if TimesOveride != 0:
            if times >= 10:
                return None
        else:
            if times >= TimesOveride:
                return None
            
        try:
            self.sounds[name]
            if overide == True:
                self.sounds[name]=directory
            else:
                AddSound(name,directory,times=times+1)
        except:
            self.sounds[name]=directory
    def PlaySound(self,sound):
        playsound(self.sounds[sound],block=False)
    def MapAll(self,command):
        keys = list("qwertyuiopasdfghjklzxcvbnm")
        for key in keys:
            self.Bind(key,command)
            print(key)
        

game.sounds['Tap-1'] = "C:\Users\Not Shownin\Desktop\GameMaker\v0.1\Tap-1.mp3"
game.sounds['Tap-2'] = "C:\Users\Not Shownin\Desktop\GameMaker\v0.1\Tap-2.mp3"


from random import randint
def Tap(event):
    print("tap")
    if randint(1,2) == 1:
        game.PlaySound("Tap-1")
    else:
        game.PlaySound("Tap-1")

I know it is a lot but there is lots more.

A break down of my code is that it just adds the direcotory to the sounds dictionary so i can play it later.

furas
  • 134,197
  • 12
  • 106
  • 148
charsarg
  • 61
  • 3

1 Answers1

6

I managed to solve my own question!

from tkinter import *
from tkinter import ttk
from playsound import playsound
import threading


class GameMaker:
    def __init__(self,root=None):
        self.sounds={}
    def AddSound(self,name,directory,overide=False,times=0,TimesOveride=0):
        if times != 0:
            name = name + str(times)
        if TimesOveride != 0:
            if times >= 10:
                return None
        else:
            if times >= TimesOveride:
                return None
            
        try:
            self.sounds[name]
            if overide == True:
                self.sounds[name]=directory
            else:
                AddSound(name,directory,times=times+1)
        except:
            self.sounds[name]=directory
    def PlaySound(self,sound):
        play = threading.Thread(target=playsound, args=(self.sounds[sound],))
        play.start()
    def MapAll(self,command):
        keys = list("qwertyuiopasdfghjklzxcvbnm")
        for key in keys:
            self.Bind(key,command)
            print(key)
        

game.sounds['Tap-1'] = "C:\Users\Not Shownin\Desktop\GameMaker\v0.1\Tap-1.mp3"
game.sounds['Tap-2'] = "C:\Users\Not Shownin\Desktop\GameMaker\v0.1\Tap-2.mp3"


from random import randint
def Tap(event):
    print("tap")
    if randint(1,2) == 1:
        game.PlaySound("Tap-1")
    else:
        game.PlaySound("Tap-1")

The only changes I made were:

  1. I added a new import
    import threading
    
  2. And I defined and started a thread:
    play = threading.Thread(target=playsound, args=(self.sounds[sound],))
    play.start()
    
Sylvester Kruin
  • 3,294
  • 5
  • 16
  • 39
charsarg
  • 61
  • 3