PySimpleGUI/DemoPrograms/Demo_Input_Validation.py

27 lines
817 B
Python
Raw Normal View History

import PySimpleGUI as sg
2018-12-19 15:01:01 +00:00
"""
Simple field validation
Input field should only accept digits 0-9.
2018-12-19 15:01:01 +00:00
If non-digit entered, it is deleted from the field
Copyright 2022 PySimpleGUI
2018-12-19 15:01:01 +00:00
"""
layout = [[sg.Text('Enter digits:')],
[sg.Input('', enable_events=True, key='-INPUT-')],
[sg.Button('Ok', key='-OK-'), sg.Button('Exit')]]
2018-12-19 15:01:01 +00:00
window = sg.Window('Window Title', layout)
2018-12-19 15:01:01 +00:00
while True: # Event Loop
event, values = window.read()
print(event, values)
if event == sg.WIN_CLOSED or event == 'Exit':
2018-12-19 15:01:01 +00:00
break
# if last char entered not a digit
if event == '-INPUT-' and len(values['-INPUT-']) and values['-INPUT-'][-1] not in ('0123456789'):
# delete last char from input
window['-INPUT-'].update(values['-INPUT-'][:-1])
window.close()