5

I am a beginner on Python and even more beginner on PySimpleGui, which I enjoy so much playing around in creating small apps with a GU interface. The issue I am having is about the table element. So far, I managed to populate the table element with data from a sqlite database. Once the table is ready with data, I can highlight a specific row. As I am not aware of any event triggered by the table element, I placed a button to be clicked once a row has been selected and then execute the code associated to click event. Table ready, row selected, button clicked, and now, how to get the entire row values ? I tried "values['mytable']" but the result were somehow like table index. I would like to have a list of values. Is it possible ? Thanks for any help.

layout:

data_values = []
data_headings = ['File ID', 'Type', 'Description', 'Remarks']
data_values.append(['', '', '', ''])
data_cols_width = [5, 8, 35, 35]
tab5_layout = [

[sg.Table(values=data_values, headings=data_headings,
                            max_col_width=65,
                            col_widths=data_cols_width,
                            auto_size_columns=False,
                            justification='left',
                            num_rows=6, key='_filestable_')],
    
[sg.Button('Select Row', key='_rowselected_')]

event:

if event == '_rowselected_':
    te1 = values['_filestable_']
    print('Event triggered : ', te1)

2 Answers 2

8

When working with elements you've not used before in PySimpleGUI, it's helpful to look through several of the materials provided by the project.

One is the Demo Programs. These are meant to give you a jump-start on using features. The other is the call-reference documentation. You can access this same information via the docstrings (Control+Q when using PyCharm).

I am not aware of any event triggered by the table element

If an element is capable of producing an event and doesn't do so by default (like a button or a menu), then you'll find a parameter enable_events.

For the Table element, you'll find the docstring (and thus the call reference also) has a parameter for the table element:

enable_events – Turns on the element specific events. Table events happen when row is clicked

In your example, you need to do is add this parameter to your Table in the layout.

[sg.Table(values=data_values, headings=data_headings,
                            max_col_width=65,
                            col_widths=data_cols_width,
                            auto_size_columns=False,
                            justification='left',
                            enable_events=True,
                            num_rows=6, key='_filestable_')],

EDIT:

I like to provide answers that teach how to solve the problems rather than just blurting out an answer. This will help you not just in this situation, but others as well when using the PySimpleGUI package.

Let's dig another step deeper though to understand how the values entry for Tables works.

The entry in the values dictionary for Tables is a LIST of the rows selected.

Be cautious if you see a construct such as this:

data_values[values['_filestable_']]

as it won't run. It will crash.

The values cannot be used to directly lookup values in your source table. Trying to do so will generate an error that list indexes must be integers or slices.

Since events have been enabled in this example for the table, you can check for the event in your event loop and appropriately handle the event:

if event == '_filestable_':
    data_selected = [data_values[row] for row in values[event]]

data_selected will be a LIST of the rows of your original data.

If you wish to limit your user to selecting only 1 row at a time, then you can set the select_mode parameter when creating your table.

select_mode=sg.TABLE_SELECT_MODE_BROWSE

Browse will allow single rows rather than the default of multiple rows. With the select mode of BROWSE set, you can assume your data_selected list will only have 1 entry, and thus data_selected[0] will be that entry. Or you can use data_selected[0] to get only the first row selected.

One final word...

The PySimpleGUI documentation has a section on support that begins by explaining that sites such as SO are not recommended. The recommendation is that these be avoided for numerous reasons, including a basic lack of knowledge. For pretty much all GitHub projects that are active, filing an Issue with that project tends to provide high-quality answers. Most devs want to help and want to know of problems or common questions that perhaps mean the documentation isn't clear.

Good luck with your program! Hope it all works out well for you.

3
  • 1
    @Mike from PSG While this is useful information about PySimpleGUI, this is not an answer to the question. Saying to read the docs, or the Demos, or providing further info didn't actually solve the original problem like mine did.
    – akaButters
    Jun 20, 2021 at 0:24
  • 1
    @Marcell Accepting the this as an answer, when it didn't actually resolve your initial question is kind of wrong IMHO.
    – akaButters
    Jun 20, 2021 at 1:03
  • Thanks for the detailed description. I would like to know is there a way to obtain both the row and column of the selected cell?
    – Borut Flis
    Jun 29, 2021 at 6:36
3

Here you go:

This will give you the values of the selected row after you click the button.

# --- EVENT LOOP ---
while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED:
        break
    if event == '_rowselected_':
        print(data_values[values['_filestable_']])

4
  • 2
    Right on. It returns as [3], so you need to extract the 0th element to get the value. print( data_values[ values['_filestable_'][0] ] ) Jun 18, 2021 at 2:46
  • @Doyousketch2 That's right. I left that part out because you can have several things in a row and you can pick which you want.
    – akaButters
    Jun 18, 2021 at 2:48
  • Also, there should be a way to break from yer event loop, but that's beside the main point. , [ sg.CButton( 'Cancel' ) ] if event == sg.WIN_CLOSED or event == 'Cancel': break Jun 18, 2021 at 2:50
  • 1
    @Doyousketch2 Right again. I didn't include something outside the main question. But your comment is correct, that's how you'd do it. In fact I'll add it to the answer be extra clear.
    – akaButters
    Jun 18, 2021 at 2:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.