Pysimplegui从另一个窗口更新主窗口元素

发布于 2025-01-19 19:49:04 字数 3675 浏览 2 评论 0原文

嗨,新手Python,在这里进行学校项目 ScreenShot

我目前有一个辅助窗口,可以为各个主题注册结果。

单击“提交”按钮后,我应该在事件循环中添加什么,以便它将主窗口(右表)上的主题分数更新为从辅助窗口注册的各个学生?

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

Hi new to python here and doing a school project
Screenshot

I currently have a secondary window that will register the results for individual subjects.

After clicking the submit button, what should I add in the event loop so that it will update the subject scores on the main window (right table) to the respective students that were registered from the secondary 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深居我梦 2025-01-26 19:49:04

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

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

Here, use arguments to call function to create a new popup window.
After close this popup window, return the values you need in the main window to do the update.

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

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文