Deleting/Hiding PySimpleGui 中 table 的一列

Deleting/Hiding a column of a table in PySimpleGui

我在 PySimpleGui (sg.Table) 中得到了一个 table,我想将选项添加到 hide/delete table 的一列。我找到了一种使用 table 的数据执行此操作的方法,但您似乎无法即时编辑 table 的标题。至少.update()方法没有提供。

我的第一个想法:使用新数据创建一个新的 window,但不包含该列。但这似乎是一种费力的做事方式...

有什么巧妙的方法吗?

您可以使用选项 displaycolumns 配置 ttk.Treeview (sg.Table) 到 select 实际显示哪些列并确定它们的显示顺序。如果您指定 display_row_numbers=True.

,请在您的 displaycolumns 列表前再添加一个 'Row'
from copy import deepcopy
import PySimpleGUI as sg

sg.theme("DarkBlue3")

newlist = [
    [f"Cell ({row:0>2d}, {col:0>2d})" for col in range(8)]
        for row in range(10)
]

COL_HEADINGS = ["Date", "Ref", "ID", "Owner", "Customer", "Price", "Size", "Path"]

layout = [
    [sg.Table(
        values=newlist,
        headings=COL_HEADINGS,
        max_col_width=25,
        num_rows=10,
        alternating_row_color='green',
        display_row_numbers=True,
        key='-TABLE-',
        enable_click_events=True,
        justification='center',
    )],
    [sg.Button('Hide Customer')],
]

window = sg.Window('Table', layout, finalize=True)
table = window['-TABLE-']

while True:

    event, values = window.read()
    if event == sg.WIN_CLOSED:
        break
    elif event == 'Hide Customer':
        window[event].update(disabled=True)
        displaycolumns = deepcopy(COL_HEADINGS)
        displaycolumns.remove('Customer')
        table.ColumnsToDisplay = displaycolumns
        table.Widget.configure(displaycolumns=['Row']+displaycolumns)

window.close()