简体   繁体   中英

Deleting/Hiding a column of a table in PySimpleGui

I got a table in PySimpleGui (sg.Table) and I want to add the option to hide/delete a column of that table. I found a way to do this with the data of the table, but it seems you can't edit the headings of a table on the fly. At least the .update() method doesn't provide one.

My first thought: Creating a new window with new data, without that column. But that seems like a laborious way of doing things...

Is there any clever way to do it?

You can configure ttk.Treeview (sg.Table) with option displaycolumns to select which columns are actually displayed and determines the order of their presentation.

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',
        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=displaycolumns)

window.close()

在此处输入图像描述

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM