【问题标题】:Deleting/Hiding a column of a table in PySimpleGui在 PySimpleGui 中删除/隐藏表的列
【发布时间】:2021-12-07 16:44:53
【问题描述】:

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

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

有什么巧妙的办法吗?

【问题讨论】:

    标签: python pysimplegui


    【解决方案1】:

    您可以使用选项displaycolumns 配置 ttk.Treeview (sg.Table) 以选择实际显示的列并确定它们的显示顺序。如果您指定了display_row_numbers=True,则在显示列列表前再添加一个'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()
    

    【讨论】:

    • 非常感谢!还有一件事:如果我使用行号 (display_row_numbers=True) 并使用您的解决方案,行号就会消失。你知道为什么吗?或者对此有解决方案?
    • 更新如上。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-26
    相关资源
    最近更新 更多