PermissionError:[Errno 13]权限被拒绝:' file.mp3'

时间:2016-03-16 20:10:30

标签: python python-2.7 python-3.x pygame mp3

程序只运行两次。之后,发生错误。我不知道它为什么会工作两次然后停止。

import tkinter, sys, pygame
from tkinter import messagebox
from gtts import gTTS

soundfile="file.mp3"

def ex():
    sys.exit()

主要问题是:

def read():
    t = e.get()
    tts = gTTS(text=t, lang="en")
    tts.save(soundfile)
    pygame.mixer.init(frequency=16000, size=-16, channels=2, buffer=4096)
    pygame.mixer.music.load(soundfile)
    pygame.mixer.music.set_volume(1.0)
    pygame.mixer.music.play(0,0.0)
    while pygame.mixer.music.get_busy()==True:
        continue
    pygame.quit()

接下来是按钮的代码。

def clear():
    e.delete(0, 'end')

main = tkinter.Tk()

e = tkinter.Entry(main, justify = "center")
l = tkinter.Label(main, text = "Write text")
b1 = tkinter.Button(main, text = "Read", command = read)
b2 = tkinter.Button(main, text = "Clear", command = clear)
b3 = tkinter.Button(main, text = "Exit", command = ex)

所以,我没有任何想法来修复它。

1 个答案:

答案 0 :(得分:4)

from gtts import gTTS
import playsound
import os

x = ['sunny', 'sagar', 'akhil']
tts = 'tts'
for i in range(0,3):
    tts = gTTS(text= x[i], lang = 'en')
    file1 = str("hello" + str(i) + ".mp3")
    tts.save(file1)
    playsound.playsound(file1,True)
    os.remove(file1)

为每个新的保存重命名该文件,这对我有用。