我有一个代码,它将语音识别中的文本显示到文本框中。Tkinter中的python语音识别
问题:它只是听一次然后停止running.I需要listen it till,i close the Tkinter
。
如果我说清除那么它必须清除文本框中的内容。 我的问题是,我不能直接告诉Tkinter的内容。它在Shell输出后监听。
请帮我解决我的问题。
编码:
from Tkinter import *
import pyaudio
import tkMessageBox
import Tkinter as tki
import tkFileDialog as th1
import speech_recognition as sr
r = sr.Recognizer()
with sr.Microphone() as source:
audio = r.listen(source)
try:
a=(r.recognize(audio))
print a
except LookupError:
a=("Could not understand audio")
print a
class App(object):
def __init__(self,root):
self.root = root
# create a Frame for the Text and Scrollbar
txt_frm = tki.Frame(self.root, width=900, height=900)
txt_frm.pack(fill="both", expand=True)
# ensure a consistent GUI size
txt_frm.grid_propagate(False)
# create first Text label, widget and scrollbar
self.lbl1 = tki.Label(txt_frm, text="Type")
self.lbl1.grid(row=0,column=0,padx=2,pady=2)
self.txt1 = tki.Text(txt_frm, borderwidth=3, relief="sunken", height=4,width=55)
self.txt1.config(font=("consolas", 12), undo=True, wrap='word')
self.txt1.grid(row=25, column=7, sticky="nsew", padx=2, pady=2)
self.txt1.insert(0.0,a)
def clearBox(self):
if a == "clear":
self.txt1.delete('1.0', 'end')
root = tki.Tk()
app = App(root)
root.mainloop()
您需要线程来解决这个问题,并且由于Tkinter不知道线程,所以需要后续函数来轮询mainloop中的更改/识别。 – deets 2014-11-03 17:04:24
@deets你能帮我一个小例子,涉及我的查询吗? – 2014-11-03 17:24:23
@sarkite:如果你已经让PyAudio使用speech_recognition工作,就像你在这里完成的那样,你能不能关闭或者更好地回答你关于它的其他问题。 http://stackoverflow.com/questions/26666216/pyaudio-build-and-install-python-portaudio-bindings-first – tom10 2014-11-03 21:10:57