0

The program given below is working properly but the looping portion is not functioning properly. This is the error message: PermissionError: [Errno 13] Permission denied: 'TALK.mp3'. The program only works for first time and when I reset it, no voice output comes and this exception shows. Can anyone explain to me what is wrong with the code?

from tkinter import *
from gtts import gTTS
from playsound import playsound

root = Tk()
root.geometry('400x400')
root.config(bg = 'ghost white')
root.title('TEXT_TO_SPEECH')

Label(root, text = 'TEXT_TO_SPEECH' , font='arial 20 bold' , bg ='white smoke').pack()

Label(root, text ='Enter Text', font ='arial 15 bold', bg ='white smoke').place(x=15,y=70)

Msg = StringVar()

entry_field = Entry(root,textvariable =Msg, width ='50')
entry_field.place(x=18 , y=100)

def Text_to_speech():
    Message = entry_field.get()
    speech = gTTS(text = Message)
    speech.save('TALK.mp3')
    playsound('TALK.mp3')

def Exit():
    root.destroy()

def Reset():
    Msg.set('')

Button(root, text = "PLAY" , font = 'arial 15 bold', command = Text_to_speech, width =4).place(x=25,      y=140)
Button(root,text = 'EXIT',font = 'arial 15 bold' , command = Exit, bg = 'OrangeRed1').place(x=100,y=140)
Button(root, text = 'RESET', font='arial 15 bold', command = Reset).place(x=175 , y =140)

root.mainloop()
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
RarKru
  • 3
  • 2
  • 1
    may not be related but it is not the best practice to put method after class. You should have a variable and in a new line do `variable_name.place(**args)`. actually Your code needs better formatting in general – Matiiss Apr 01 '21 at 10:48
  • ok ill try writing in better format for my next programs ^^ – RarKru Apr 01 '21 at 22:21

1 Answers1

0

Just try to use os.remove() in this way:


from tkinter import *
from gtts import gTTS
from playsound import playsound
import os

root = Tk()
root.geometry('400x400')
root.config(bg='ghost white')
root.title('TEXT_TO_SPEECH')

Label(root, text='TEXT_TO_SPEECH', font='arial 20 bold', bg='white smoke').pack()

Label(root, text='Enter Text', font='arial 15 bold', bg='white smoke').place(x=15, y=70)

Msg = StringVar()

entry_field = Entry(root, textvariable=Msg, width='50')
entry_field.place(x=18, y=100)


def Text_to_speech():
    os.remove('TALK.mp3')
    Message = entry_field.get()
    speech = gTTS(text=Message)
    speech.save('TALK.mp3')
    playsound('TALK.mp3')


def Exit():
    root.destroy()


def Reset():
    Msg.set('')


Button(root, text="PLAY", font='arial 15 bold', command=Text_to_speech, width=4).place(x=25, y=140)
Button(root, text='EXIT', font='arial 15 bold', command=Exit, bg='OrangeRed1').place(x=100, y=140)
Button(root, text='RESET', font='arial 15 bold', command=Reset).place(x=175, y=140)

root.mainloop()
Funpy97
  • 282
  • 2
  • 9