Creating a Table Element GUI Application Using Python PySimpleGUI – Step-by-Step Tutorial

Posted by


In this tutorial, we will create a Table Element GUI app using PySimpleGUI. PySimpleGUI is a Python library that allows you to easily create GUI applications with minimal code. The Table Element is a powerful widget that allows you to display data in a tabular format, similar to a spreadsheet.

To get started, make sure you have PySimpleGUI installed. You can install it using pip:

pip install PySimpleGUI

Once you have PySimpleGUI installed, you can start creating your Table Element GUI app. Here is a step-by-step guide to help you get started:

Step 1: Import the necessary libraries
First, you need to import the PySimpleGUI library and any other libraries that you plan to use in your app. In this case, we will also import the random library to generate some sample data.

import PySimpleGUI as sg
import random

Step 2: Define the layout of the GUI app
Next, you need to define the layout of your GUI app. In this example, we will create a simple table with three columns: Name, Age, and Country. We will also include buttons to add rows and delete selected rows.

layout = [
    [sg.Table(values=[], headings=['Name', 'Age', 'Country'],
               auto_size_columns=False, max_col_width=25,
               display_row_numbers=True, justification='right',
               num_rows=10, enable_events=True, key='-TABLE-')],
    [sg.Button('Add Row'), sg.Button('Delete Selected Row')]
]

Step 3: Create the PySimpleGUI window
Now, you need to create the PySimpleGUI window using the layout you defined in the previous step.

window = sg.Window('Table Element GUI App', layout)

Step 4: Generate sample data and update the table
Next, we will generate some sample data and update the table with this data.

data = []
for i in range(10):
    name = f'Person {i+1}'
    age = random.randint(18, 60)
    country = random.choice(['USA', 'Canada', 'UK'])
    data.append([name, age, country])

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

Step 5: Handle events
Finally, you need to handle events such as button clicks and table selections. In this example, we will handle the events for adding rows and deleting selected rows.

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

    if event == sg.WIN_CLOSED:
        break

    if event == 'Add Row':
        name = f'Person {len(data)+1}'
        age = random.randint(18, 60)
        country = random.choice(['USA', 'Canada', 'UK'])
        data.append([name, age, country])
        window['-TABLE-'].update(values=data)

    if event == 'Delete Selected Row':
        selected_rows = values['-TABLE-']
        if selected_rows:
            data = [row for index, row in enumerate(data) if index not in selected_rows]
            window['-TABLE-'].update(values=data)

Step 6: Close the window
Once the user closes the window, make sure to close it properly to avoid any memory leaks.

window.close()

That’s it! You have created a simple Table Element GUI app using PySimpleGUI. Feel free to customize the layout and functionality of the app to suit your needs. Happy coding!