PySimpleGui 表行单击事件

发布于 2025-01-18 20:48:24 字数 1925 浏览 0 评论 0原文

得分表

python的新手并在学生得分管理系统上进行学校项目。

我想在单击表行时更新右窗口上的文本以与学生的名字相对应。

我应该在活动循环中添加什么才能实现这一目标?

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

Score table

New to python and doing a school project on student score management system.

I want to update the text on the right window to correspond with the student's name when I click the table row.

What should I add to my event loop to achieve that?

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

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

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

发布评论

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

评论(1

淡淡的优雅 2025-01-25 20:48:24

我认为,您的第一个问题是在 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

祝您好运。

i think, the first of your problem is on the sg.table instead of

enable_click_events=True

it should be:

enable_events=True

in that way, you can catch the event hello and you will get the elements you want something like this:

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

now, it is already written in the above code, but to update the sg.frame do it this way:

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

you will have to change the -key- according what you want to update.

the whole code i used for testing:

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

good luck.

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