PySimpleGui table 行点击事件

PySimpleGui table row click event

Score table

刚接触python,正在做一个关于学生成绩管理系统的学校项目。

我想在单击 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']


activeStudent = [
    [sg.Text("Name:"),sg.Text('',enable_events=True,key='-activeStudent-')],
    [sg.Text("Math Result:"), sg.Text('', enable_events=True, key='-math-')],
    [sg.Text("English Result:"), sg.Text('', enable_events=True, key='-english-')],
    [sg.Text("Science Result:"), sg.Text('', enable_events=True, key='-science-')],
]


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='hello',
              row_height=55,
              tooltip='Results',
              enable_click_events=True),sg.Frame('',activeStudent,size=(800,550))],
    ]

resultsWindow = sg.Window("Register Results", result, size=(1000, 500), finalize=True, )

while True:
    event, values = resultsWindow.read()
    if event == "Submit" or event == sg.WIN_CLOSED:
        break

我认为,您的第一个问题是 sg.table 而不是

enable_click_events=True

应该是:

enable_events=True

通过这种方式,您可以捕获事件 hello 并且您将获得您想要的元素,如下所示:

if event == "Submit" or event == sg.WIN_CLOSED:
    break
elif event == "hello":
    data_selected = [nameList[row] for row in values[event]]
    print(data_selected)  # an array like this [[6, 'Fischl', 4.0]]
    resultsWindow['-activeStudent-'].update(data_selected[0][1])
else:
    continue

现在,它已经写在上面的代码中,但是要更新 sg.frame 这样做:

resultsWindow['-activeStudent-'].update(data_selected[0][1])

您必须根据要更新的内容更改 -key-。

我用于测试的全部代码:

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.6],
            [3, nameDict["A3"], 3.7],
            [4, nameDict["A4"], 3.8],
            [5, nameDict["A5"], 3.9],
            [6, nameDict["A6"], 4.0],
            [7, nameDict["A7"], 4.1],
            [8, nameDict["A8"], 4.2],
            [9, nameDict["A9"], 4.3],
            [10, nameDict["A10"], 4.4]]

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


activeStudent = [
    [sg.Text("Name:"), sg.Text('', enable_events=True,key='-activeStudent-')],
    [sg.Text("Math Result:"), sg.Text('', enable_events=True, key='-math-')],
    [sg.Text("English Result:"), sg.Text('', enable_events=True, key='-english-')],
    [sg.Text("Science Result:"), sg.Text('', enable_events=True, key='-science-')],
]


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='hello',
              row_height=55,
              tooltip='Results',
              enable_events=True), sg.Frame('', activeStudent, size=(800, 550))],
    ]

resultsWindow = sg.Window("Register Results", result, size=(1000, 500), finalize=True, )

while True:
    event, values = resultsWindow.read()

    if event == "Submit" or event == sg.WIN_CLOSED:
        break
    elif event == "hello":
        data_selected = [nameList[row] for row in values[event]]
        print(data_selected)
        resultsWindow['-activeStudent-'].update(data_selected[0][1])
    else:
        continue

祝你好运。