【问题标题】:Sorting table by columns按列排序表
【发布时间】:2021-08-31 13:50:40
【问题描述】:

这个小程序从 csv 文件中抓取数据并显示

【问题讨论】:

    标签: python python-3.x pysimplegui


    【解决方案1】:

    目前没有找到您点击哪个标题的现有方法,因此此处需要 tkinter 代码。

    下面demo代码中双击表头排序,表格如何排序,取决于你的排序代码,这里不展示。

    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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-15
      • 1970-01-01
      • 1970-01-01
      • 2021-07-19
      • 2021-07-14
      • 2011-10-29
      • 1970-01-01
      相关资源
      最近更新 更多