我不确定以前是否已经回答过这个问题,如果是的话,很抱歉重复了一次,但是我在任何地方都找不到它
我正在为我的简单AIML聊天机器人制作GUI(主要用于娱乐目的) 我找到了PySimpleGui。我阅读了它的全部文档,并尝试使用它们的代码,将其实现到我自己从教程中获得的小代码中
原来:
kernel = aiml.Kernel()
kernel.learn("std-startup.xml")
kernel.respond("load aiml b")
while True:
input_text = input("You: ")
response = kernel.respond(input_text)
print("Csigusz Foxoup (bot): "+response)
我得到了这个代码的工作,一切都很好(谢谢米斯巴)
我让我的机器人在cmd里准确地说了几句话。 接下来,我想添加一个简单的gui
我更希望它看起来更健谈,但由于缺乏编码经验,我所能想到的只是一个带有两个按钮和两条文本的简单窗口
cood看起来像这样:
import aiml
import PySimpleGUI as sg
kernel = aiml.Kernel()
kernel.learn("std-startup.xml")
kernel.respond("load aiml b")
sg.theme('LightBlue 1')
layout = [[sg.Text('You: '), sg.Text(size=(12,1), key='-mytext-')],
[sg.Text('Csigusz Foxoup (bot): '), sg.Text(size=(12,1), key='-CSI-')],
[sg.Input(key='-myinput-')],
[sg.Button('Send message'), sg.Button('Bye!')]]
window = sg.Window('Csigusz Foxoup, your friend in a box (bot)', layout, [200,400])
while True:
event = window.read()
values = window.read()
if event == sg.WIN_CLOSED or event == 'Bye!':
break
if event == 'Send message':
# change the "output" element to be the value of "input" element
input_text = (values)
response = kernel.respond(input_text)
window['-mytext-'].update(values['-myinput-'])
print("Csigusz Foxoup(bot): "+response)
window.close()
它为我提供了一扇漂亮的小窗户looks like this
我的问题是,当我键入某个内容并单击按钮时,什么都没有发生。当我按下close window(X)时,我收到一条错误消息:“您已经尝试了100次读取一个关闭的窗口,您需要添加一个检查事件==WIN_closed,error”
既然我有一张支票,还有一个按钮,我不知道为什么它不起作用。我也不知道如何才能得到按钮,发送我的机器人的用户文本,然后检索机器人的输出
我做错了什么?感谢您提前回复。非常感谢大家的帮助!🙏
你的问题是你用错了
.read()
您只需使用一个
.read()
,它以tuple(event, values)
的形式返回两个值最小工作代码(不带
aiml
)相关问题 更多 >
编程相关推荐