事件 PySimpleGUI 后更新 table 个值

Update table values after event PySimpleGUI

我有 window,其中包含一个由 sqllite 数据库填充的 table。 在 table 中选择一行后,我想删除数据库行显示一个确认弹出窗口并刷新 table 以反映。 选择行、返回值、更新数据库和显示弹出窗口的过程……但是我无法让 table 反映更改并收到此错误。

Traceback (most recent call last):
  File "/home/ryan/VT_Projects/record_collection_gui/input_tab.py", line 152, in <module>
    row_index = values["-RECORDSTABLE-"][0]
IndexError: list index out of range

删除函数和主循环:

def delete_record(selected_row: list) -> None:
    db.delete_db_item(table="records_data", key=selected_row[0])
    sg.popup(f"Deleted {selected_row[1]} {selected_row[2]}", title="Delete successful")


def edit_record(selected_row: list, evnt, val) -> None:
    pass


window = sg.Window("Vinyl Catalogue", table_layout, modal=True)

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

    if event == "-RECORDSTABLE-":
        row_index = values["-RECORDSTABLE-"][0]
        selected_row_values = table_array[row_index]
    else:
        pass

    if event == "-DELETERECORD-":
        try:
            delete_record(selected_row_values)
            window["-RECORDSTABLE-"].update(values=table_array)
        except NameError:
            sg.popup_error("Please select row to delete", title="Error")

    elif event == "-EXPORTCSV":
        export_csv(table_array)
    
    elif event == "-QUIT-":
        break

window.close()

感谢您的帮助!

已解决!

已处理 IndexError 删除后因缺失值引起的问题。 使用 refresh() 更新 table post 删除中的值。

import PySimpleGUI as sg
from database import db_querys as db

def delete_record(table: list, row: list, index: int) -> None:
    db.delete_db_item(table="records_data", key=row[0])
    del table[index]


window = sg.Window("Vinyl Catalogue", table_layout)

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

    if event == "-RECORDSTABLE-":
        try:
            row_index = values["-RECORDSTABLE-"][0]
            selected_row = table_array[row_index]
        except IndexError:
            continue

    if event == "-DELETERECORD-":
        try:
            delete_record(table_array, selected_row, row_index)
            window['-RECORDSTABLE-'].update(table_array)
            sg.popup(f"Deleted {selected_row[1]} {selected_row[2]}", title="Delete successful")
        except NameError:
            sg.popup_error("Please select row to delete", title="Error")


    elif event == "-EXPORTCSV":
        export_csv(table_array)

    elif event == "-QUIT-":
        break

window.close()