-1

So I am working on an encryption software with PySimpleGUI and I'm trying to add a live text update feature. It's when the output updates as I type in the input box. For some reason it's not working and it only works when I use a button to update the output.

All other stuff works the only problem is the while loop.

Button (this works):

layout = [
        [sg.Text('Enter a string to encode or decode')],
        [sg.InputText(key='input')],
        [sg.Text('Encode or Decode')],
        [sg.Combo(values=['Encode', 'Decode'], default_value='Encode', key='encodedecode'), sg.Button('Submit')],
        [sg.Multiline(size=(40, 10), key="output")],
        [sg.Button('Exit')]
        ]
window = sg.Window("Encryption", layout, icon=get_ico())

while True:
    try:
        event, values = window.read()
        if event == sg.WIN_CLOSED or event == 'Exit':
            break
        if event == 'Submit':
            if values['encodedecode'] == 'Encode':
                encoded_text = encode(values['input'])
                window['output'].update(encoded_text)
                print(encoded_text)
            elif values['encodedecode'] == 'Decode':
                decoded_text = decode(values['input'])
                window['output'].update(decoded_text)
                print(decoded_text)
        
        time.sleep(0.3)
    except Exception as e:
        print(e)
        print("An error has occured")

This is what I have right now:

layout = [
        [sg.Text('Enter a string to encode or decode')],
        [sg.InputText(key='input')],
        [sg.Text('Encode or Decode')],
        [sg.Combo(values=['Encode', 'Decode'], default_value='Encode', key='encodedecode')],
        [sg.Multiline(size=(40, 10), key="output")],
        [sg.Button('Exit')]
        ]
window = sg.Window("Encryption", layout, icon=get_ico())

while True:
    try:
        event, values = window.read()
        if event == sg.WIN_CLOSED:
            break

        if values['encodedecode'] == 'Encode':
            encoded_text = encode(values['input'])
            window['output'].update(encoded_text)
            print(encoded_text)
        elif values['encodedecode'] == 'Decode':
            decoded_text = decode(values['input'])
            window['output'].update(decoded_text)
            print(decoded_text)
        
        time.sleep(0.3)
    except Exception as e:
        print(e)
        print("An error has occured")

I expected the output to update every time I typed but instead I got a blank output. Every time the output doesn't change at all.

Phantom
  • 1
  • 1
  • I'd recommend [Eel](https://github.com/python-eel/Eel) which allows you to use Python but use HTML/JS/CSS for the GUI. This is much easier and much better supported. – futium Aug 16 '23 at 17:40
  • I would use Eel but I'm not really that familiar with HTML and CSS and really only know NodeJS or Python – Phantom Aug 16 '23 at 17:43
  • completely fair point. i would say that for most use cases, it's easier to familiarize yourself with HTML/CSS than fight with PySimpleGUI since its not hard to learn enough HTML/CSS to accomplish your goals – futium Aug 16 '23 at 17:49
  • Add option `enable_events=True` in your `InputText` element, then it will work. – Jason Yang Aug 16 '23 at 18:51
  • Thank you so much! `enable_events=True` made it work! – Phantom Aug 17 '23 at 17:45

0 Answers0