Pysimplegui从另一个窗口更新主窗口元素
嗨,新手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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这里,使用参数调用函数来创建一个新的弹出窗口。
关闭此弹出窗口后,返回主窗口中需要的值以进行更新。
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.