单击按钮运行speech_recognition时UI将冻结



我创建了一个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()

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

最新更新