【问题标题】:Python text to speech (pyttsx3) delay when i use it with tkinter当我将它与 tkinter 一起使用时,Python 文本到语音(pyttsx3)的延迟
【发布时间】:2017-07-30 05:27:58
【问题描述】:

我正在尝试学习 GUI,我想将一个简单的聊天机器人作为一个项目,它也可以说话(使用 pyttsx3 库)......但是当它说话时,它先说话然后停止,然后是在 GUI 窗口中将其标记出来。总而言之......它会稍微延迟......这是代码

import random
import tkinter as tk
import pyttsx3
engine = pyttsx3.init()

def speak(ui):
    engine.say(ui)
    engine.runAndWait()
    engine.stop()

root = tk.Tk()
user_input = tk.Entry(root)
user_input.grid(row = 0, column = 1)
root.geometry("200x200")
greetings = ['hola', 'hello', 'hi', 'Hi', 'hey!', 'hey']
question = ['How are you?', 'How are you doing?']
responses = ['Okay', "I'm fine"]
huh = "I did not understand what you said"

def cb():
    user_text = user_input.get()
    if user_text in greetings:
        bot_text = random.choice(greetings)
        speak(bot_text)
    elif user_text in question:
        bot_text = random.choice(responses)
        speak(bot_text)
    else:
        bot_text = huh
    output.config(text=bot_text)

button = tk.Button(root, text="Enter", command=cb)
button.grid(row = 0)

output = tk.Label(root, text='')
output.grid()

tk.mainloop()

有没有办法解决这个问题?提前致谢!

【问题讨论】:

    标签: python-3.x user-interface tkinter


    【解决方案1】:

    我对这个概念很陌生,但我认为您正在寻找的是多线程。通过在两个单独的线程上打开 GUI 和 bot,它们可以同时运行。

    【讨论】:

      【解决方案2】:

      这是一个旧线程,但可能对某人有用:不要在这个简单的任务中使用线程,你会让这个过程变得无用的繁重,只需在按钮中使用 Lambda 和你的命令:

      button = tk.Button(root, text="Enter", command=lambda: cb())
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-05-13
        • 2020-04-29
        • 1970-01-01
        • 1970-01-01
        • 2014-09-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多