【发布时间】: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