我创建了一个有两个键的字典,当选择其中一个键时,字典项被更新,问题是我没有在更新的列表中返回所选的值。例如,当选择"男性",然后选择"执行"时,我希望接收"执行"作为值
import PySimpleGUI as sg
genero = {
'male': ['Required','Executed'],
'female': ['Required', 'Performed']
}
layout = [
[sg.Radio('male', "RADIO1", default=False, key="-IN1-")],
[sg.Radio('female', "RADIO1", default=False, key="-IN2-")],
[sg.Listbox(genero.keys(), size=(30, 3), enable_events=True, key='-PART-')],
[sg.Push(),sg.Button('GENERATE'), sg.Exit("Exit")]
]
window = sg.Window("GENERATE PETITION", layout)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED or event == "Exit":
break
elif values["-IN1-"] == True:
window['-PART-'].update(genero['male'])
elif values["-IN2-"] == True:
window['-PART-'].update(genero['female'])
elif event == 'GENERATE':
print('-PART-')
window.close()
print(event,values)
atualmente esta retornando相互矛盾:退出{"三机一体——":真的,"in2——":假的,"- - - -":[]}
在事件循环中存在编程逻辑问题,对于所有情况,从事件决策开始,而不是值(s)决策,这将是更好的。在您的代码中,事件GENERATE
的case将永远不会在单击任何一个Radio元素后执行。
import PySimpleGUI as sg
genero = {
'male': ['Required','Executed'],
'female': ['Required', 'Performed']
}
layout = [
[sg.Radio('male', "RADIO1", default=False, enable_events=True, key="-IN1-")],
[sg.Radio('female', "RADIO1", default=False, enable_events=True, key="-IN2-")],
[sg.Listbox(genero.keys(), size=(30, 3), enable_events=True, key='-PART-')],
[sg.Push(),sg.Button('GENERATE'), sg.Exit("Exit")]
]
window = sg.Window("GENERATE PETITION", layout)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED or event == "Exit":
break
elif event in ('-IN1-', '-IN2-'):
if values["-IN1-"] == True:
window['-PART-'].update(genero['male'])
else:
window['-PART-'].update(genero['female'])
elif event == 'GENERATE':
selections = values['-PART-']
if selections:
print(selections[0])
else:
print('Nothing selected !')
window.close()