Python 在tkinter上运行语音识别使其冻结

Python 在tkinter上运行语音识别使其冻结,python,python-3.x,tkinter,speech-recognition,Python,Python 3.x,Tkinter,Speech Recognition,我正在写一个程序,你可以和它交谈,它的反应就像siri。我用谷歌语音识别和espeak来听和说。然后将对话打印到文本框中 当程序被要求使用语音识别进行监听时,它会冻结并再次单击GUI,然后它会说“没有响应”,这是我运行的错误还是无法在tkinter中运行语音识别 为什么这么冷 以下是我迄今为止编写的全部代码: import tkinter as tk from subprocess import call as say import winsound import speech_recognit

我正在写一个程序,你可以和它交谈,它的反应就像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()

运行脚本时,计算机一次只能执行一项操作。因此,例如,如果您希望它侦听,则计算机在执行当前命令时将无法运行任何其他命令。解决这个问题的方法是使用多个线程,这样您的计算机可以同时做两件事。查看Python的线程模块。老实说,我对这方面也不太了解,但这是我在自己的GUI中实现的

import tkinter as tk
from subprocess import call as say
import winsound
import speech_recognition as sr

import threading

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):
    def callback(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)

    a_thread = threading.Thread(target = callback(tex))
    a_thread.start()

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()

基本思想是创建一个线程对象,然后给它一个要运行的函数。然后,当您想要运行该线程时,调用该线程的
start()
方法。这是肯定的,因为一次运行多个线程可能会出现问题。

这是因为您的计算机一次只能做一件事。当它在监听时,它无法处理任何GUI操作。为了防止这种情况发生,请尝试使用多个线程来解决此问题。@Dzhao如何添加多个线程?对不起,我是python新手。刚刚更新了我的答案。行
a_thread=threading.thread(target=callback(tex))
需要一个
callback
函数的输入。Dzhao先生能帮我解决这个问题吗?好的,谢谢,我将大胆地阅读线程,因为看起来我会经常使用它。