Tips for Filling Tables with Inputs using PySimpleGUI

Posted by


PySimpleGUI is a powerful and easy-to-use Python library for creating graphical user interfaces. One common task when working with GUI applications is populating a table with data from user inputs. In this tutorial, we will cover how to do this using PySimpleGUI’s Table element.

  1. Installation:
    Before we start, make sure you have PySimpleGUI installed. You can install PySimpleGUI using pip:
pip install PySimpleGUI
  1. Import PySimpleGUI:
    Next, we need to import PySimpleGUI in our Python script:
import PySimpleGUI as sg
  1. Create the GUI window:
    Let’s create a simple GUI window with two input fields for the user to enter data and a button to add the data to a table.
layout = [
    [sg.InputText(), sg.InputText(), sg.Button('Add')],
    [sg.Table(values=[], headings=['Column 1', 'Column 2'], auto_size_columns=False, col_widths=[10, 10])]
]

window = sg.Window('Table Filling', layout)
  1. Create the event loop:
    Now, we need to create an event loop to handle user inputs and update the table with the entered data.
table_data = []
while True:
    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break

    if event == 'Add':
        if values[0] and values[1]:
            # Add the data to the table
            table_data.append([values[0], values[1]])
            window['Table'].update(values=table_data)
  1. Run the GUI:
    Finally, we need to run the GUI by calling the read() method on the window object.
window.close()

And that’s it! You now have a simple GUI application that allows users to enter data into input fields and populate a table with the entered data. You can expand on this example by adding functionality to edit or delete rows in the table, or by saving the table data to a file.

Overall, PySimpleGUI makes it easy to create interactive GUI applications in Python, and filling tables from user inputs is just one of the many tasks you can accomplish with this powerful library. I hope this tutorial has been helpful in getting you started with PySimpleGUI and building GUI applications in Python.

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

This sample code data will disappear after exiting. This video shows how to save your data https://youtu.be/BaIKzvZf-qk
Changed github example to clear boxes with
for i in range(3): # Loop thru to clear boxes
window[Headings[i]].update(value='')

@luisfelipe1169
2 months ago

Olá, como faz para pegar todos os elementos de uma coluna da tabela, e passar para uma lista

@aavalos163
2 months ago

Thank you for posting this video. It was very helpful and easy to understand.

@wendygrant2735
2 months ago

Keith, thanks for the explanation. I realize that I have/had a struggle with that 'backward thinking' to get it going.
I tried it out and yes there it is. Next step was indeed saving the input values. You already helped me out on that one.
So the next challenge will be using these values in the table and do some math in formulas with them and then return
these calculations data into a new table. Fun ahead for sure. Thank you for sharing your knowledge.
I have a strong believe in sharing knowledge for free as you do and so do I do the same.
I had people in my home a couple of times to learn brewing and making cheese. It was always fun to get 'm going.

@itsmeintorrespain2714
2 months ago

I might try this as an addition to the forthcoming PJW weather station exercise. I've been running a simple weather station for the last 10 years outside on my balcony using MYSQL. I wonder if PSG has the same ability to retrieve, say, Max Min temperature data for a given period, eg 24hrs?