CatChMeIfUCan
CatChMeIfUCan

Reputation: 569

how to set a input text as required pysimplegui

I have a layout and an input text and I want the input text don't be left blank I have searched all PySimpleGUI documentation but didn't see how I am able to set an input text as required

here is my code

layout = [[sg.Text('enter your license code')],
              [sg.InputText()], # I wan't this input to be required
              [sg.Submit('OK'), sg.Cancel('Cancel')]]
    window = sg.Window('invalid License', layout, icon="logo.ico")
    while True:
        event, values = window.read()
        if event == 'Cancel' or event == sg.WIN_CLOSED:
            break  # exit button clicked
    window.close()
    license_input = values[0]
    read_configs('license.txt')
    lic = "license.txt"
    with open(lic, 'r+') as f:
        text = f.read()
        text = re.sub(license_code, license_input, text)
        f.seek(0)
        f.write(text)
        f.truncate()

Upvotes: 0

Views: 4389

Answers (1)

Jason Yang
Jason Yang

Reputation: 13061

A license_code required for a popup window

  • 'Cancel' button not necessary and removed
  • 'Close' button of window disabled by enable_close_attempted_event=True and sg.WINDOW_CLOSE_ATTEMPTED_EVENT not handled, or it will destroy window first when 'Close' button clicked.
  • Read and return the current value of the input element by method get, or by using values[element] if not 'Close' button of window event.
  • Break event loop only when value of input element not null string.
import PySimpleGUI as sg

layout = [
    [sg.Text('enter your license code')],
    [sg.InputText(key='-INPUT-')],
    [sg.Submit('OK')],
]
window = sg.Window('invalid License',
    layout,
    #icon="logo.ico",
    enable_close_attempted_event=True)

while True:
    event, values = window.read()
    print(event, values)
    license_code = window['-INPUT-'].get().strip()
    if event == 'OK' and license_code:
        break

window.close()
print(license_code)

Upvotes: 1

Related Questions