Home > OS >  PySimpleGui updating dictionary with button event
PySimpleGui updating dictionary with button event

Time:04-07

new to python and doing a school project

Interface

I have an interface that registers the results of each student for various subjects and I would like to seek help on what my event loop should do to update the results into my dictionary after clicking the submit button.

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":
        ...

CodePudding user response:

Update the dictionary only when submit for the selected student name.

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()
  • Related