【发布时间】:2014-12-30 09:28:56
【问题描述】:
我有一个代码,它将从语音识别中显示文本到文本框。
问题:它只听一次然后停止运行。我需要listen it till,i close the Tkinter。
如果我说 clear 那么它必须清除文本框中的内容。 我的问题是,我不能直接将内容告诉 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 不知道线程,你需要后函数来轮询你的主循环中的更改/识别。
-
@deets 你能帮我举个小例子吗,涉及我的查询?
-
@sarkite:如果您已经让 PyAudio 与 Speech_recognition 一起工作,就像您在这里所做的那样,请您关闭或更好地回答您关于它的其他问题。 stackoverflow.com/questions/26666216/…
-
关于一个完全不相关的话题.. 你能告诉我你是如何让 Speech_recognition 工作的吗?我不断收到
no module named speech_recognition
标签: python loops tkinter speech-recognition