0

I have an input element for phone numbers, and I want to check, if the input has only numbers, however, the inputs always are strings. How can I check the inputs, before they are converted to strings?

cool_guy
  • 41
  • 3

1 Answers1

0

Following code demo two ways to validate entry, one by method isdecimal of str and another one by option validatecommand of tkinter Entry widget.

import PySimpleGUI as sg

def validate(text):
    return True if text.isdecimal() else False  # Return True if valid input

layout = [
    [sg.Input(key='-IN1-')],
    [sg.Input(key='-IN2-')],
    [sg.Push(), sg.Button('Submit')],
]
window = sg.Window('Title', layout, finalize=True)
vcmd = (window.TKroot.register(validate), '%P')
window['-IN1-'].widget.configure(validate='all', validatecommand=vcmd)

while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED:
        break
    elif event == 'Submit':
        answer = '' if values['-IN2-'].isdecimal() else 'not '
        print(f"Characters in second Input element are {answer}all numbers.")

window.close()
Jason Yang
  • 11,284
  • 2
  • 9
  • 23