Edit and Delete Table Rows with PySimpleGUI

Posted by


PySimpleGUI is a simple yet powerful GUI library for Python that allows you to quickly create user interfaces for your applications. In this tutorial, we will focus on how to implement row editing and deletion in a Table element using PySimpleGUI.

To get started, you will need to install PySimpleGUI if you haven’t already. You can do this by running the following command in your terminal:

pip install PySimpleGUI

Now let’s create a simple application that displays a Table element with the ability to edit and delete rows.

import PySimpleGUI as sg

# Create a list of data for the Table
data = [
    ['John Doe', 30, 'john.doe@example.com'],
    ['Jane Smith', 25, 'jane.smith@example.com']
]

# Define the layout of the window
layout = [
    [sg.Table(values=data, headings=['Name', 'Age', 'Email'], key='-TABLE-', auto_size_columns=True)],
    [sg.Button('Edit'), sg.Button('Delete')],
    [sg.Button('Exit')]
]

# Create the window
window = sg.Window('Table Row Edit and Delete', layout)

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

    if event == sg.WIN_CLOSED or event == 'Exit':
        break

    # Handle row editing
    if event == 'Edit':
        selected_row = values['-TABLE-'][0]
        new_data = sg.popup_get_text('Enter new data (Name, Age, Email)', default_text=','.join(data[selected_row])).split(',')
        data[selected_row] = new_data
        window['-TABLE-'].update(values=data)

    # Handle row deletion
    if event == 'Delete':
        selected_row = values['-TABLE-'][0]
        data.pop(selected_row)
        window['-TABLE-'].update(values=data)

# Close the window
window.close()

In this code snippet, we first create a list of data that will be displayed in the Table element. We then define the layout of the window, which consists of the Table element, an Edit button, a Delete button, and an Exit button.

In the event loop, we check for the user’s actions. When the Edit button is clicked, we prompt the user to enter new data for the selected row, update the data list, and refresh the Table element. When the Delete button is clicked, we remove the selected row from the data list and update the Table element accordingly.

Finally, we close the window when the user closes it or clicks the Exit button.

You can run this code and test the functionality of editing and deleting rows in the Table element. You can further customize the layout and functionality to suit your needs and requirements.

I hope this tutorial helps you understand how to implement row editing and deletion in a Table element using PySimpleGUI. If you have any questions or need further assistance, feel free to ask. Happy coding!

0 0 votes
Article Rating
5 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@vladimirkostov8182
2 months ago

Great video! Can you make tutorial with editing a row by selecting a row clicking edit button and new window popping up and from that new window to edit the old info? Thank you!

@laoalex4525
2 months ago

I'm starting to study python and for me this is the best example of using pysimplegui on youtube, the most complete one.
Thank you, I learned a lot from your example. Congratulations !

@hasanaqbayli149
2 months ago

Great Explanation!!! Please tell me, can we use Database as SQL for this example and add search function to filter data from the Table tree? Thanks in advance

@henkoegema6390
2 months ago

Well done Keith.😄

@wendygrant2735
2 months ago

Very very useful! Thanks Keith.