Dears,
如何根据另一个选择字段值Python/PySimpleGUI禁用输入字段。这个脚本的目的是:一旦从第一个";组合列表";,它将禁用相应的entery,第二个或第三个,或者两者都禁用,下面是我的脚本实现这一点的尝试,不幸的是,需要在事件循环中进行额外的调整才能实现。你能在这方面提供支持吗:
import PySimpleGUI as sg
layout = [
[sg.Text('Filed 1:', size=(15,1)), sg.Combo(['DisableField2', 'DisableField3',
'DisableField2&3'], size=(25, 1), key='FIELD1')],
[sg.Text('Field 2:', size=(15,1)), sg.Input(key='FIELD2', size=(25,1))],
[sg.Text('Field 3:', size=(15,1)), sg.Input(key='FIELD3', size=(25,1))],
],
window = sg.Window('MAIN', layout, finalize=True,element_justification='c')
while True:
event, values = window.read()
print(event, values)
if event == sg.WIN_CLOSED or event == 'Exit':
break
elif event == 'FIELD1':
window['FIELD1'].update(result2.upper())
if event == 'DisableField2':
window['FIELD2'].update(disabled=True)
elif event == 'DisableField3':
window['FIELD3'].update(disabled=True)
elif event == 'DisableField2&3':
window['FIELD2'].update(disabled=True)
window['FIELD2'].update(disabled=True)
window.close()
使用元素的方法update(disabled=True)
禁用元素。
当enable_events=True
时,该事件将是Combo元素的关键'FILED1'
。
要禁用哪个元素是由Combo元素values['FIELD1']
的值决定的,而不是由事件决定的。
import PySimpleGUI as sg
settings = ['DisableField2', 'DisableField3', 'DisableField2&3']
layout = [
[sg.Text('Filed 1:'), sg.Combo(settings, size=10, expand_x=True, enable_events=True, key='FIELD1')],
[sg.Text('Field 2:'), sg.Input(key='FIELD2', size=25, disabled_readonly_background_color="Gray")],
[sg.Text('Field 3:'), sg.Input(key='FIELD3', size=25, disabled_readonly_background_color="Gray")],
]
window = sg.Window('MAIN', layout, finalize=True, element_justification='c')
while True:
event, values = window.read()
if event == sg.WIN_CLOSED or event == 'Exit':
break
print(event, values)
if event == 'FIELD1':
setting = values[event]
if setting == 'DisableField2':
window['FIELD2'].update(disabled=True)
window['FIELD3'].update(disabled=False)
elif setting == 'DisableField3':
window['FIELD2'].update(disabled=False)
window['FIELD3'].update(disabled=True)
elif setting == 'DisableField2&3':
window['FIELD2'].update(disabled=True)
window['FIELD3'].update(disabled=True)
window.close()