在按钮cli上运行语音识别时,UI冻结

2024-06-08 16:51:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我创建了一个python文件,当单击开始时,micrphone应该工作并开始说话,如果我停止,它应该将声音转换为文本并添加到文本框中,但是每次单击开始,应用程序都崩溃了!你知道吗

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()

Tags: textimportfieldsourceaswindowwidthaudio