当单击按钮时运行Speech_recognition时,UI冻结

时间:2019-11-04 18:02:03

标签: python tkinter onclick speech-recognition speech-to-text

我创建了一个python文件,单击“开始”时,麦克风应该可以工作并开始通话;如果我停止了,它应该将语音转换为文本并将其添加到文本框中,但是每次单击“开始”,应用程序便崩溃了!

import tkinter as tk
import speech_recognition as sr


window = tk.Tk()
window.title("Voice to Text")
window.geometry("300x300")


def startvoice():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        try:
            audio = r.record(source)
            voice2text = r.recognize_google(audio)
            text_field.focus()
            text_field.delete()
            text_field.insert(0, voice2text)
        except:
            print("error")


button1 = tk.Button(text="Start", width=16, command=startvoice)
button1.grid(column=0, row=0)

text_field = tk.Text(master=window, height=20, width=40)
text_field.grid(column=0, row=1)


window.mainloop()

1 个答案:

答案 0 :(得分:0)

您需要在单独的线程中运行r.record(source)及以下版本。