Web Analytics Made Easy - Statcounter
Skip to content

Input

Input call reference

The Input element enables users to enter a single line of text. If you need to be able to enter multiple lines of text, then use a Multiline element.

import PySimpleGUI as sg

layout = [[sg.Input(default_text='Hello! o/', key='-INPUT-)]]

Events

If the parameter enable_events=True then any key press when the Input element has focus will generate an event. The value of the Input does not have to change for this event to be generated.

Values Dictionary

The values dictionary entry is the string currently shown in the Input element.

get Method

values['-INPUT ELEMENT KEY-'] contains the value of the string in the Input element when the event happened that caused window.read() to return. You do not need to call window['-INPUT ELEMENT KEY-'].get() to obtain the value of the element.

With most GUI frameworks, a widget's value is obtained by calling a get method of some sort. The Input element does have a get method, but you should not need to call it except in uncommon situations. The point of the values dictionary is so that you do not need to call a get method. If you must have the most current value, that may have changed since your last call to window.read, then perhaps calling get is justified.

Passwords

This element is ideal for entering passwords. The parameter password_char is used to specify a character that will be used to obscure characters entered by the user. Most password entry windows use the ""*" character to hide passwords. If you set password_char='*' then as characters are entered into the Input element, rather than seeing the characters being typed, each character is instead displayed as *.

Creating "Forms" - the do_not_clear parameter

This parameter makes creating forms easier. When inputing information into a form, the normal sequence of events is that all of the fields are cleared after a form is "added" or "submitted". If you create your Input element with do_not_clear=False then your Input element will be set to an empty string after every window.read() call. It's equivalent to calling window['-INPUT ELEMENT KEY-'].update('')

Input Validation

It's not uncommon to want to restrict your user to entering only certain characters. For example, if you're expecting only an integer to be entered, then you can add this validation to your event loop by enabling events for the Input element, checking the value, and if illegal characters are present - remove them and write the value back to the Input element by calling its update method.