【发布时间】:2016-04-01 02:26:21
【问题描述】:
我正在编写一个程序,你可以与它交谈并且它会像 siri 一样响应。我正在使用谷歌的语音识别和espeak 来听和说。然后将对话打印到文本框。
当程序被要求使用语音识别进行收听时,它会冻结并再次单击 GUI,然后显示“没有响应”,是我运行错了还是无法在 tkinter 中运行语音识别?
为什么会结冰?
这是我到目前为止编写的全部代码:
import tkinter as tk
from subprocess import call as say
import winsound
import speech_recognition as sr
def cbc(tex):
return lambda : callback(tex)
def callback(tex):
button = "Listen"
tex.insert(tk.END, button)
tex.see(tk.END)# Scroll if necessary
def listen(tex):
say('espeak '+"Say,,your,,command,,after,,the,,beep", shell=True)
winsound.Beep(1000,500)
ltext = 'listening...'
tex.insert(tk.END, ltext)
tex.see(tk.END)
r = sr.Recognizer()
with sr.Microphone() as source:
damand = r.listen(source)
damandtxt = (recognizer_google(damand))
tex.insert(tk.END, damandtxt)
tex.see(tk.END)
top = tk.Tk()
tex = tk.Text(master=top)
tex.pack(side=tk.RIGHT)
bop = tk.Frame()
bop.pack(side=tk.LEFT)
tk.Button(bop, text='Listen', command=lambda: listen(tex)).pack()
tk.Button(bop, text='Exit', command=top.destroy).pack()
top.mainloop()
【问题讨论】:
-
这是因为您的计算机一次只能做一件事。当它在监听时,它不能处理任何 GUI 操作。为了防止这种情况发生,请尝试使用多个线程来解决此问题。
-
@Dzhao 如何添加多个线程?抱歉,我对 python 很陌生。
-
刚刚更新了我的答案。
a_thread = threading.Thread(target = callback(tex))行需要callback函数的输入。 -
stackoverflow.com/questions/56343553/… Dzhao 先生能帮我解决这个问题吗?
标签: python python-3.x tkinter speech-recognition