Python 如何让PySimpleGui读取输入并更新窗口?
我不确定以前是否已经回答过这个问题,如果是的话,很抱歉重复了一次,但是我在任何地方都找不到它 我正在为我的简单AIML聊天机器人制作GUI(主要用于娱乐目的) 我找到了PySimpleGui。我阅读了它的全部文档,并尝试使用它们的代码,将其实现到我自己从教程中获得的小代码中 原来:Python 如何让PySimpleGui读取输入并更新窗口?,python,user-interface,input,text,pysimplegui,Python,User Interface,Input,Text,Pysimplegui,我不确定以前是否已经回答过这个问题,如果是的话,很抱歉重复了一次,但是我在任何地方都找不到它 我正在为我的简单AIML聊天机器人制作GUI(主要用于娱乐目的) 我找到了PySimpleGui。我阅读了它的全部文档,并尝试使用它们的代码,将其实现到我自己从教程中获得的小代码中 原来: kernel = aiml.Kernel() kernel.learn("std-startup.xml") kernel.respond("load aiml b") w
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()
它为我提供了一扇漂亮的小窗户
我的问题是,当我键入某个内容并单击按钮时,什么都没有发生。当我按下close window(X)时,我收到一条错误消息:“您已经尝试了100次读取一个关闭的窗口,您需要添加一个检查事件==WIN_closed,error”
既然我有一张支票,还有一个按钮,我不知道为什么它不起作用。我也不知道如何才能得到按钮,发送我的机器人的用户文本,然后检索机器人的输出
我做错了什么?感谢您提前回复。非常感谢大家的帮助 你的问题是你用错了
.read()
您只需使用一个.read()
,它以元组(事件,值)
的形式返回两个值
最小工作代码(无
aiml
)
为什么要使用
.read()
两次-第一次事件
,第二次值
?也许先用print()
看看你在变量中得到了什么,现在看来确实有效了,谢谢!另一方面,我有一个新的东西,我找不到它做什么,那就是我的机器人得到了这样一条信息:“值:{'-myinput-':'hugs'}”我如何解决这个问题?你能再帮我一点忙吗?谢谢你,现在它工作了!一切看起来都很好。输入和输出工作,并响应。谢谢你的帮助!
event, values = window.read()
print('event:', event)
print('values:', values)
import PySimpleGUI as sg
sg.theme('LightBlue 1')
layout = [[sg.Text('You: '), sg.Text(size=(50,1), key='-mytext-')],
[sg.Text('Csigusz Foxoup (bot): '), sg.Text(size=(50,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, values = window.read()
print('event:', event)
print('values:', values)
if event == sg.WIN_CLOSED or event == 'Bye!':
break
if event == 'Send message':
input_text = values['-myinput-']
response = "some response for " + input_text
#response = kernel.respond(input_text)
window['-mytext-'].update(input_text)
window['-CSI-'].update(response)
window.close()