Skip to content Skip to sidebar Skip to footer

Sorting Table By Columns

This little program grabs data from a csv file and displays

Solution 1:

There's no existing method, at this moment, to find which header you click, so tkinter code required here.

Double clicks for header sorting in following demo code, how to sort the table, it depend on your code for sorting and not shown here.

import PySimpleGUI as sg


def double_click(event):
    """
    Additional event for double-click on header
    event: class event
    """
    region = table.identify("region", event.x, event.y)
    if region == 'heading':                                 # Only care double-clock on headings
        cid = int(table.identify_column(event.x)[1:])-1     # check which column clicked
        window.write_event_value("-TABLE-DOUBLE-CLICK-", cid)

data = [
    ["Name",         "Cases/All", "Case/Day", "Deaths/All", "Death/Day"],
    ["Global",       "80773033",    "563983",    "1783619",     "11784"],
    ["USA",          "19147627",    "174814",     "332423",      "1779"],
    ["India",        "10244852",     "20549",     "148439",       "286"],
    ["Brazil",        "7504833",     "20548",     "191570",       "431"],
    ["Russian",       "3131550",     "26513",      "56426",       "599"],
    ["France",        "2530400",     "11295",      "63701",       "969"],
    ["UK",            "2382869",     "53135",      "71567",       "458"],
    ["Italy",         "2067487",     "11210",      "73029",       "659"],
    ["Spain",         "1893502",      "7717",      "50442",        "36"],
    ["Germany",       "1687185",     "22459",      "32107",      "1129"],
    ["Colombia",      "1603807",      "9310",      "42374",       "203"],
    ["Argentina",     "1590513",      "6586",      "42868",       "218"],
    ["Mexico",        "1389430",      "5996",     "122855",       "429"],
    ["Turkey",        "1364242",     "15805",      "20388",       "253"],
    ["Poland",        "1281414",     "12780",      "28019",       "565"],
    ["Iran",          "1212481",      "6108",      "54946",       "132"],
    ["Ukraine",       "1045348",      "7986",      "18324",       "243"],
    ["South Africa",  "1021451",      "9580",      "27568",       "497"],
    ["Peru",          "1008908",      "1251",      "37525",        "51"],
    ["Netherlands",    "778293",      "7561",      "11218",       "171"],
]

sg.theme('DarkBlue')
sg.set_options(font='Courier 11')

layout = [
    [sg.Table(data[1:], headings=data[0], auto_size_columns=False,
        def_col_width=13, enable_events=True, key='-TABLE-')],
]

window = sg.Window('Table', layout, finalize=True)
table = window['-TABLE-'].Widget
table.bind('<Double-1>', double_click, add='+')

while True:
    event, values = window.read()
    if event == sg.WINDOW_CLOSED:
        break
    elif event == '-TABLE-DOUBLE-CLICK-':
        column = values[event]
        print(f'Click on column {column}')
        # Sort data on the table by the value of column
        # then update window['-TABLE-'].update(values=new_data)

window.close()

Post a Comment for "Sorting Table By Columns"