输入限制,需要即时对输入信息进行验证。如果输入不允许,则不允许输入。
可以通过enable_events进行即时验证。如果最近输入的字符不允许,则将输入框内容退格。
import PySimpleGUI as sg
layout = [[sg.Text('Input passworld:'), sg.Input(key='-PASSWORLD-', enable_events=True)],
[sg.Button('EXIT')]]
window = sg.Window('', layout)
while True:
event, values = window.read()
if event in [sg.WIN_CLOSED, 'EXIT']:
break
# 输入框事件,且输入框内容不为空时,如果不符合,便退格。
if event == '-PASSWORLD-' and values['-PASSWORLD-'] and values['-PASSWORLD-'][-1] not in '0123456789':
window['-PASSWORLD-'].update(values['-PASSWORLD-'][:-1])
window.close()