PySimpleGUI – Tables That Can Be Sorted

Posted by


PySimpleGUI is a Python library that makes it easy to create user interfaces for desktop applications. One of the many features of PySimpleGUI is the ability to create sortable tables. In this tutorial, we will walk through how to create a sortable table using PySimpleGUI.

Step 1: Install PySimpleGUI

Before we can start creating our sortable table, we need to install PySimpleGUI. You can install PySimpleGUI using pip by running the following command in your terminal:

pip install PySimpleGUI

Step 2: Create the Table

Now that we have PySimpleGUI installed, we can start creating our sortable table. The first step is to import the PySimpleGUI library and create the layout for our table. Here’s an example layout for a table with two columns:

import PySimpleGUI as sg

layout = [[sg.Table(values=[], headings=['Column 1', 'Column 2'], key='-TABLE-', enable_events=True)]]
window = sg.Window('Sortable Table', layout)

In this layout, we have created a table with two columns and assigned it the key ‘-TABLE-‘. We have also set enable_events=True so that we can capture events from the table, such as when a column header is clicked to sort the table.

Step 3: Populate the Table

Next, we need to populate the table with data. We can do this by updating the values attribute of the table element. Here’s an example of how you can populate the table with some sample data:

table_data = [
    ['Row 1 Col 1', 'Row 1 Col 2'],
    ['Row 2 Col 1', 'Row 2 Col 2'],
    ['Row 3 Col 1', 'Row 3 Col 2']
]

window['-TABLE-'].update(values=table_data)

In this example, we have created a list of lists with the data for each row in the table. We then update the values attribute of the table element with this data, which will populate the table with the data.

Step 4: Sort the Table

To make the table sortable, we need to capture events when a column header is clicked and sort the data accordingly. We can do this by adding an event loop to our code that listens for events from the table. Here’s an example of how you can handle sorting the table when a column header is clicked:

while True:
    event, values = window.read()

    if event in (sg.WIN_CLOSED, 'Cancel'):
        break

    if event == '-TABLE-':
        column_clicked = values[event][1]
        table_data.sort(key=lambda x: x[column_clicked])
        window['-TABLE-'].update(values=table_data)

In this event loop, we check for events from the table and handle sorting the table when a column header is clicked. We extract the index of the column that was clicked from the event data and then sort the table data based on that column. Finally, we update the table with the sorted data.

Step 5: Run the Application

To run the application and see your sortable table in action, add the following code to the end of your script:

window.close()

Now, you can run your script and see your sortable table in action. You can click on the column headers to sort the table data alphabetically.

And that’s it! You have successfully created a sortable table using PySimpleGUI. This is just one example of what you can do with PySimpleGUI, so feel free to explore the library further and create more complex user interfaces for your desktop applications. Happy coding!

0 0 votes
Article Rating

Leave a Reply

3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@ahmedjoo1858
7 days ago

great video, Thanks, …I just added a global variable "sortingType" as Boolean to the "sorted" command to be able to switch between ASC and DESC sort order, the "sortingType" switch between true and false with each call of the function, so one click to the header sorts ASC then the second click sorts DESC .. works beautiful.

@keithlohmeyer
7 days ago

Very nice! Thanks for pointing out the operator module as it is new to me. I have always used lambda functions for sort keys like this
table = sorted(table, key=lambda col: col[col_clicked])
Thanks for the tutorial!

@muralidharan.m645
7 days ago

A nice beginner friendly video 🤩

3
0
Would love your thoughts, please comment.x
()
x