首页 > 解决方案 > Tkinter 的 Mainloop 函数不允许其他函数运行

问题描述

我实际上是 NLI(Novitatis Locus Industries)的成员,我们的团队似乎遇到了一些问题。在使用 GUI 设计支持语音的程序时,tkinter 的 mainloop 似乎给我们带来了一些问题。查看我们的代码-

//this function defines the gui//
def gui():
gui_kurooto = Tk()
txt_dsply_var = StringVar()
AUTHN_LBL = Label(gui_kurooto, text=txt_dsply_var)
AUTHN_LBL.pack()
gui_kurooto.mainloop()

//this code defines the microphone input// 
def takeCommand():
#It takes microphone input from the user and returns string output
r = sr.Recognizer()
with sr.Microphone() as source:
    print("Listening...")
    txt_dsply_var = StringVar()
    txt_dsply_var.set('Listening')//display listening on gui//

    r.pause_threshold = 1
    r.energy_threshold = 10000
    audio = r.listen(source)
try:
    print("Recognizing...")
    txt_dsply_var = StringVar()
    txt_dsply_var.set('Recognizing')//display recognizing//

    query = r.recognize_google(audio, language='en-en')
    print(f"User said: {query}\n")
    txt_dsply_var = StringVar()
    txt_dsply_var.set(f"User said: {query}\n")//display input//
except Exception as e:
    # print(e)
    print("Say that again please...")
    return "None"
return query

//to run//
if __name__ == "__main__":
gui()
    while True:
# if 1:
    query = takeCommand().lower()

现在,当我尝试运行它时,代码永远不会到达 while 循环。我知道主循环是一个无限循环,但是否有替代品存在,所以我可以让它达到其他功能。将功能保持在功能之外也是行不通的。我可以把它放在查询命令之后吗?

另外我还有其他问题,有没有一种方法可以根据语音输入进行光谱分析,例如 Google 和 Siri 有在我们说话时移动的条形?

任何帮助,将不胜感激。

标签: pythontkinter

解决方案


推荐阅读