pysamplegui通过事件(event)触发窗口读取。事件可以是点击按钮,点击文本等。
在之前的例子中,通过点击submit按钮,触发窗口读取。
现可通过 enable_events 实现即时响应。在输入内容时,便进行响应。
下例中,输入框输入密码,点击提交后,显示密码。

import PySimpleGUI as sg
layout = [ [sg.Text('Please input passworld'), sg.Input(size=(10,1), key='-PASSWORLD-')],
[sg.Text('Your passworld is:'), sg.Text(size=(10,1), key='-show-')],
[sg.Button('Submit')]]
window = sg.Window('', layout)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
if event == 'Submit':
passworld = values['-PASSWORLD-']
window['-show-'].update(passworld)
window.close()
下例中,输入框即时响应。在输入密码时,如果密码长度小于6,则提示too short;如果长度大于等于6,则显示密码。






import PySimpleGUI as sg
# 设置输入框enable_events=true,即时触发
layout = [ [sg.Text('Please input passworld'), sg.Input(size=(10,1), key='-PASSWORLD-', enable_events=True)],
[sg.Text('Your passworld is:'), sg.Text(size=(10,1), key='-show-')]]
window = sg.Window('', layout)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
passworld = values['-PASSWORLD-']
if len(passworld) > 5:
window['-show-'].update(passworld)
else:
window['-show-'].update('too short')
window.close()