PySimpleGui 正在从另一个 Window 更新主 window 元素

PySimpleGui Updating Main window elements from another Window

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

我目前有一个中学 window 可以登记个别科目的成绩。

点击提交按钮后,我应该在事件循环中添加什么,以便它将主window(右table)上的科目分数更新到已注册的各个学生来自中学 window?

import PySimpleGUI as sg

nameDict = {'A1': 'Albedo',
            'A2': 'Barbara',
            'A3': 'Chongyun',
            'A4': 'Diluc',
            'A5': 'Eula',
            'A6': 'Fischl',
            'A7': 'Ganyu',
            'A8': 'Hu Tao',
            'A9': 'Jean',
            'A10': 'Kazuha'}

nameList = [[1, nameDict["A1"], 3.5],
            [2, nameDict["A2"], 3.5],
            [3, nameDict["A3"], 3.5],
            [4, nameDict["A4"], 3.5],
            [5, nameDict["A5"], 3.5],
            [6, nameDict["A6"], 3.5],
            [7, nameDict["A7"], 3.5],
            [8, nameDict["A8"], 3.5],
            [9, nameDict["A9"], 3.5],
            [10, nameDict["A10"], 3.5]]

headings = ['Index', 'Name', 'Cumulative GPA']

resultsOptions = [
    [sg.Button("Statistics")]
]

activeStudentResults = [
    [sg.Text("Math:"),sg.Text('',enable_events=True,key='-Math-')],
    [sg.Text("Science:"),sg.Text('',enable_events=True,key='-Science-')],
    [sg.Text("English:"),sg.Text('',enable_events=True,key='-English-')],
    [sg.Text("Chinese:"),sg.Text('',enable_events=True,key='-Chinese-')],
]
activeStudent = [
    [sg.Text("Name:"),sg.Text('',enable_events=True,key='-activeStudent-')],
    [sg.Frame("Results", activeStudentResults, border_width=5, size =(500,500))]
]

result = [
    [sg.Table(values=nameList, headings=headings, max_col_width=100,
              auto_size_columns=True,
              display_row_numbers=False,
              justification='center',
              num_rows=10,
              key='Table',
              row_height=55,
              tooltip='Results',
              enable_events=True), sg.Frame('', activeStudent, size=(800, 550))],
    [sg.Button("Register Results"),
     sg.Column(resultsOptions),
    ]]

def results(): #creates a popup window to input student test results
    GradesDictionary = {}
    subjects = ["English", "Math", 'Science', "Chinese"]
    studentNames = ['Albedo', 'Barbara', 'Chongyun', 'Diluc', 'Eula', 'Fischl', 'Ganyu', 'Hu Tao', 'Jean', 'Kazuha']

    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}



    resultsWindow.close()

window = sg.Window("Student Score Management System", result, size=(800, 700),use_custom_titlebar=True)

while True:
    event, values = window.read()
    if event == "Exit" or event == sg.WIN_CLOSED:
        break
    elif event == "Register Results":
        results()
    elif event == "Statistics":
        runStatistics()
    elif event == "Table":
        data_selected = [nameList[row] for row in values[event]]
        print(data_selected)
        window['-activeStudent-'].update(data_selected[0][1])
    else:
        continue

在这里,使用参数调用函数来创建一个新的弹出窗口window。 关闭此弹出窗口后 window,return 您需要在主 window 中进行更新的值。

import PySimpleGUI as sg

def popup(name):
    sg.theme('DarkBlue3')
    layout = [
        [sg.Text(f'Score for {name}')]] + [
        [sg.Text(subject, expand_x=True),
         sg.Input('', size=16, key=subject)] for subject in subjects] + [
        [sg.Button('Submit'), sg.Button('Cancel')]]
    event, values = sg.Window('Sub Win', layout, modal=True).read(close=True)
    return None if event == sg.WIN_CLOSED else {subject:values[subject] for subject in subjects}

students = ['Albedo', 'Barbara', 'Chongyun', 'Diluc', 'Eula', 'Fischl', 'Ganyu', 'Hu Tao', 'Jean', 'Kazuha']
subjects = ["English", "Math", 'Science', "Chinese"]
scores = {}

font = ('Courier New', 16)
sg.theme('DarkBlue4')
sg.set_options(font=font)

layout = [
    [sg.Text('Student'),
     sg.Combo(students, size=(10, 5), enable_events=True, bind_return_key=True, key='Student'),
     sg.Button('Update')]] + [
    [sg.Text(subject, expand_x=True),
     sg.Text('None', size=16, background_color='blue', text_color='white', key=subject)] for subject in subjects
]
window = sg.Window('Student Records', layout)

while True:

    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break

    student = values['Student']
    print(event)
    if event in ('Student', 'New Record'):
        if student in scores:
            record = scores[student]
            for subject in subjects:
                window[subject].update(record[subject])
        else:
            for subject in subjects:
                window[subject].update('None')
    elif event == 'Update':
        if student not in students:
            continue
        result = popup(student)
        if result is not None:
            scores[student] = result
            window.write_event_value('New Record', None)

window.close()