F.M
F.M

Reputation: 1859

How to get input from InputText() without a button press in PySimpleGui

Is there a way to be able to get the input from an InputText() without having to rely on a button press? I am trying to make a form where the submit button is only available when the input text is not empty, however the only way to get the input from InputText() that I have found is with a button which needs to be clicked in order for me to receive the input.

Upvotes: 1

Views: 1180

Answers (1)

Jason Yang
Jason Yang

Reputation: 13061

Decide what event by yourself to send the content of the Input element.

  • click a button - Add one button into your layout.
import PySimpleGUI as sg

layout = [[sg.Input(key='-IN-'), sg.Button('Submit')]]
window = sg.Window('Title', layout)
event, values = window.read()
if event == 'Submit':
    print(values['-IN-'])
window.close()
  • Button element with Enter key - Wth option bind_return_key=True
import PySimpleGUI as sg

layout = [[sg.Input(key='-IN-'), sg.Button('Submit', bind_return_key=True)]]
window = sg.Window('Title', layout)
event, values = window.read()
if event == 'Submit':
    print(values['-IN-'])
window.close()
  • Input element with Enter key - Binding event '<Return>' to your Input element.
import PySimpleGUI as sg

layout = [[sg.Input(key='-IN-')]]
window = sg.Window('Title', layout, finalize=True)
window['-IN-'].bind('<Return>', ' ENTER')
event, values = window.read()
if event == '-IN- ENTER':
    print(values['-IN-'])
window.close()
  • click any keyboard - With option enable_events=True
import PySimpleGUI as sg

layout = [[sg.Input(enable_events=True, key='-IN-')]]
window = sg.Window('Title', layout)
while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED:
        break
    elif event == '-IN-':
        print(values['-IN-'])
window.close()

Upvotes: 3

Related Questions