Tkinter中的Python语音识别

2 投票
1 回答
5782 浏览
提问于 2025-05-01 08:42

我有一段代码,可以把语音识别的内容显示到文本框里。

问题是:它只会听一次,然后就停止了。我需要它一直听,直到我关闭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()
暂无标签

1 个回答

4

你需要使用线程来实现同时进行的语音识别,并通过 after 方法来更新文本框的内容,因为 Tkinter 对线程的支持方式有些特别。

我这里没有语音识别的功能,所以你需要自己填补空白,把随机选择的部分替换成你实际使用的语音识别代码。

 import threading
 import time
 import random

 from Tkinter import *
 import tkMessageBox
 import Tkinter as tki
 import tkFileDialog as th1

 class SpeechRecognizer(threading.Thread):

     ANSWERS = ["foo", "bar"]

     def __init__(self):
         super(SpeechRecognizer, self).__init__()
         self.setDaemon(True)
         self.recognized_text = "initial"

     def run(self):
         while True:
             time.sleep(1.0)
             self.recognized_text = random.choice(self.ANSWERS)


 recognizer = SpeechRecognizer()
 recognizer.start()

 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.recognized_text = StringVar()
         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)
         root.after(100, self.update_recognized_text)

     def update_recognized_text(self):
         self.txt1.delete(0.0, END)
         self.txt1.insert(0.0, recognizer.recognized_text)
         root.after(100, self.update_recognized_text)

     def clearBox(self):
         if a == "clear":
             self.txt1.delete('1.0', 'end')

 root = tki.Tk()
 app = App(root)
 root.mainloop()

撰写回答