PySimpleGui 使用按钮事件更新字典

PySimpleGui updating dictionary with button event

python 新手,正在做一个学校项目

Interface

我有一个界面可以记录每个学生在不同科目上的成绩,我想寻求帮助,了解我的事件循环应该如何在单击提交按钮后将结果更新到我的字典中。

import PySimpleGUI as sg


GradesDictionary = {
    "Albedeo": {"English":None, "Math":None, "Chinese":None},
    "Barbara": {"English":None, "Math":None, "Chinese":None},
    "Chongyun": {"English":None, "Math":None, "Chinese":None}
}

subjects = ["English", "Math", 'Science', "Chinese"]
studentNames =['Albedo', 'Barbara', 'Chongyun']

resultsLayout = [[sg.Combo(studentNames, enable_events=True, key='current_student')],
      # creates a dropdown window with the student names
      [sg.Text("Name:"), sg.Text('', key='current_name', enable_events=True)],
      # displays the name of the selected student
      [sg.Text('English'), sg.InputText(do_not_clear=False)],  # standard input boxes for the score
      [sg.Text('Math'), sg.InputText(do_not_clear=False)],
      [sg.Text('Science'), sg.InputText(do_not_clear=False)],
      [sg.Text('Chinese'), sg.InputText(do_not_clear=False)],
      [sg.B("Submit"), sg.Cancel()]]  # standard button to submit score and leave window

resultsWindow = sg.Window("Register Results", resultsLayout,size=(1000,200),finalize=True,)

while True:
    event, values = resultsWindow.read()
    if event == "Cancel" or event == sg.WIN_CLOSED:
        break
    elif event == "current_student":
        resultsWindow['current_name'].update(values['current_student'])
    elif event == "Submit":
        ...

仅在提交所选学生姓名时更新字典。

import PySimpleGUI as sg


GradesDictionary = {}
subjects = ["English", "Math", 'Science', "Chinese"]
studentNames =['Albedo', 'Barbara', 'Chongyun']

layout_subjects = [
    [sg.Text(subject), sg.Push(), sg.InputText(do_not_clear=False, key=subject)]
        for subject in subjects
]
Layout = [
    [sg.Text('Select student name'),
     sg.Combo(studentNames, enable_events=True, key='current_student')],
    [sg.Column(layout_subjects)],
    [sg.B("Submit"), sg.Cancel()],  # standard button to submit score and leave window
]
resultsWindow = sg.Window("Register Results", Layout, finalize=True)

while True:
    event, values = resultsWindow.read()
    if event == "Cancel" or event == sg.WIN_CLOSED:
        break
    elif event == "Submit":
        name = values['current_student']
        if name in studentNames:
            GradesDictionary[name] = {subject:values[subject] for subject in subjects}
            print(GradesDictionary)

resultsWindow.close()