python - 当 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)
在单独的线程中运行及以下。
推荐阅读
- java - 如何定义 Maven pom.xml 以使用 org.eclipse.jetty.server.ssl.SslSocketConnector
- java - 无法从另一个 docker 容器将消息推送到部署在 docker 容器中的 Kafka 主题
- kubernetes - 将 csi 驱动程序安装到 Kubernetes 集群 v1.16 时出错
- javascript - apollo graphql 突变应该返回哪个值?
- c# - 渲染自适应卡片 Html 时,未使用数据模板
- c++ - 从 UART 接收到错误的字符?
- azure - 如何在keycloak的oidc身份提供者中映射azure object_id?
- javascript - 在 ReactJS 箭头函数类属性上防止 ESLint “no-undef”
- linux - Linux - 如何编写一个 glob 模式以匹配扩展名中至少有一个大写字符的所有文件名
- google-app-engine - 我可以在同一个 Google App Engine 项目中让不同的服务运行不同的语言吗?