【发布时间】:2021-06-06 18:19:52
【问题描述】:
我不确定之前是否已经回答过这个问题,如果是重复的,请原谅,但我无法在任何地方清楚地找到它。
我正在为我的简单 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)
我得到了这段代码,一切都很好(感谢 Misbah)
我让我的机器人在 cmd 中准确地说出一些话。 接下来我想添加一个简单的 gui。
我更希望它看起来更健谈,但由于我缺乏编码经验,我所能想到的只是一个带有 2 个按钮和 2 个文本的简单窗口。
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
我的问题是,当我键入内容并单击按钮时,什么也没有发生。当我按下关闭窗口 (X) 时,我收到一条错误消息:“您已尝试 100 次来读取关闭的窗口,您需要添加一个检查事件 == WIN_CLOSED, ERROR”
现在因为我有一个检查,还有一个按钮,我不知道为什么它不起作用。也不知道如何获得按钮以向我的机器人发送用户文本然后检索机器人输出。
我做错了什么?提前感谢您的所有回复。非常感谢所有帮助! ????
【问题讨论】:
-
你为什么使用
.read()两次——第一次是event,然后是values?也许首先使用print()看看你在变量中得到了什么
标签: python user-interface input text pysimplegui