0

我肯定是 Python 初学者,但我正在慢慢摸索出足够的方法来创建一些个人项目。我正在尝试使用 RiveScript 创建一个离线聊天机器人,到目前为止,我已经成功地让该机器人完成了我需要它做的事情。但是,我在弄清楚如何在 Tkinter 中创建 GUI,然后将其连接到聊天机器人时遇到了严重的麻烦。按照教程,我创建了一个可以启动的简单 GUI,但我没有让我的程序的两个部分一起工作。

目前,我程序中的按钮只是在控制台中启动程序,它照常工作,但没有显示我想要的位置(在 GUI 中)。

from tkinter import *
from rivescript import RiveScript
import time
rs = RiveScript()
rs.load_directory("./brain")
rs.sort_replies()

def GUI():
    # parent window
    GUI.root = Tk()

# window title, shape and resize rights
GUI.root.title("Chat Bot")
GUI.root.geometry("400x500")
GUI.root.resizable(width=TRUE, height=TRUE)

# create main menu and sub menus
main_menu = Menu(GUI.root)
main_menu.add_command(label="Quit", command=menuQuit)
GUI.root.config(menu=main_menu)

# window for chat to take place
chatwindow = Text(GUI.root, bd=1, bg="black", width="50", height="8",
                  font=("Arial", 23), foreground="#00ffff")
chatwindow.place(x=6, y=6, height=385, width=370)

# message window for text to appear
messagewindow = Text(GUI.root, bd=0, bg="black", width="30", height="4",
                     font=("Arial",23), foreground="#00ffff")
messagewindow.place(x=128, y=400, height=88, width=260)

b = Button(GUI.root, text="Send", width="12", height="5", bd=0, bg="#0080ff",
                activebackground="#00bfff", foreground="#ffffff", font=("Arial, 12"), command=mainChat)
b.place(x=6, y=400, height=88)

GUI.root.mainloop()



def mainChat():
    while True:
        moodChecker()
        mainChat.msg = input("You: ")
        reply = rs.reply("localuser", mainChat.msg)
        moodCheck()
        time.sleep(0.7)
        print("UNKNOWN: ", reply)

def menuQuit():
    quit()

我花了好几个小时翻阅类似的帖子,但还没有弄清楚我的程序哪里出了问题。我怀疑 有什么问题mainChat,因为该命令menuQuit成功退出了 GUI 程序。

4

0 回答 0