From 3995d079f0302b30f7ecf878e0a3024aba0d5636 Mon Sep 17 00:00:00 2001 From: MikeTheWatchGuy Date: Wed, 19 Dec 2018 10:01:01 -0500 Subject: [PATCH] New Demo - input validation --- DemoPrograms/Demo_Input_Validation.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 DemoPrograms/Demo_Input_Validation.py diff --git a/DemoPrograms/Demo_Input_Validation.py b/DemoPrograms/Demo_Input_Validation.py new file mode 100644 index 00000000..7b2ca404 --- /dev/null +++ b/DemoPrograms/Demo_Input_Validation.py @@ -0,0 +1,27 @@ +import sys +if sys.version_info[0] >= 3: + import PySimpleGUI as sg +else: + import PySimpleGUI27 as sg + +""" + Simple field validation + Input field should only accept digits. + If non-digit entered, it is deleted from the field +""" + +layout = [[sg.Text('Enter digits:')], + [sg.Input(do_not_clear=True, enable_events=True, key='_INPUT_')], + [sg.Button('Ok', key='_OK_'),sg.Button('Exit')]] + +window = sg.Window('Window Title').Layout(layout) + +while True: # Event Loop + event, values = window.Read() + if event in (None, 'Exit'): + break + if not len(values['_INPUT_']): # if field is empty ignore + continue + if values['_INPUT_'][-1] not in ('0123456789'): # if last char entered not a digit + window.Element('_INPUT_').Update(values['_INPUT_'][:-1]) # delete last char from input +window.Close()