How can I make PySimpleGui read my input and update my window?

I am not sure if this has been answered before, sorry for a duplicate if it was, but I couldn’t find it anywhere clearly.

I am making a GUI for my simple AIML chatbot (entertainment purposes mostly) and I found PySimpleGui. I read the whole documents of it and been trying to use their code, implementing it into my own small code I got from a tutorial.

Originally:

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)

I got this code working, all good (Thanks Misbah)

And I got my bot to say some words in the cmd accurately. Next I wanted to add a simple gui.

I’d much rather it look more chatty but all I could come up with with my lacking coding experince is a simple window with 2 buttons and 2 texts.

The cood looks like this:

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()
    

And it produces a nice little window for me. looks like this

My problem is that when I type something, and click the buttons, nothing happens. When I press close window (X) I get an error message saying: “You have tried 100 times to read a closed window, you need to add a check for event == WIN_CLOSED, ERROR”

Now since i have a check, also a button, I have no idea why it doesnt work. Also don’t know how I could get the button to send my bot the user text then retrieve the bot output.

What Am I doing wrong? Thank you for all replies in advance. All help greatly appreciated! 🙏

Answer

All your problem is that you use .read() in wrong way.

You have to use only one .read() which returns both values as tuple (event, values)

event, values = window.read()

print('event:', event)
print('values:', values)

Minimal working code (without aiml)

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()

Leave a Reply

Your email address will not be published. Required fields are marked *