Borut Flis
Borut Flis

Reputation: 16375

Can you add a hidden value inside a row in PySimpleGUI table object?

I have a table object in PySimpleGUI.

 [sg.Table(values=data_values, headings=data_headings,
                            max_col_width=65,
                            auto_size_columns=False,
                            select_mode=sg.TABLE_SELECT_MODE_BROWSE,
                            justification='left',
                            enable_events=True,
                            col_widths=[5, 5, 5,5,5,5,5,5],
                            num_rows=10, key='_tracker_')]

I have events enabled. When the table is clicked I check the row of the click and do some operations according to the values in that row. That works fine.

I now want to make one of the variables invisible but I still want to access its value when the row is clicked.

Is there a way to do this? I think somethin similar exists in HTML.

Upvotes: 1

Views: 535

Answers (1)

Jason Yang
Jason Yang

Reputation: 13061

You can use option visible_column_map to set which column to be shown or not.

visible_column_map: One entry for each column. False indicates the column is not shown.

Demo code,

import PySimpleGUI as sg

headings = ['President', 'Date of Birth']
data = [
    ['Ronald Reagan', 'February 6'],
    ['Abraham Lincoln', 'February 12'],
    ['George Washington', 'February 22'],
    ['Andrew Jackson', 'March 15'],
    ['Thomas Jefferson', 'April 13'],
    ['Harry Truman', 'May 8'],
    ['John F. Kennedy', 'May 29'],
    ['George H. W. Bush', 'June 12'],
    ['George W. Bush', 'July 6'],
    ['John Quincy Adams', 'July 11'],
    ['Garrett Walker', 'July 18'],
    ['Bill Clinton', 'August 19'],
    ['Jimmy Carter', 'October 1'],
    ['John Adams', 'October 30'],
    ['Theodore Roosevelt', 'October 27'],
    ['Frank Underwood', 'November 5'],
    ['Woodrow Wilson', 'December 28'],
]

sg.theme('DarkBlue3')
sg.set_options(("Courier New", 12))

layout = [
    [sg.Table(data, headings=headings, visible_column_map=[True, False], justification='left', select_mode=sg.TABLE_SELECT_MODE_BROWSE, enable_events=True, key='President')],
    [sg.Text('', size=(22, 1), key='Birthday')],
]
window = sg.Window("Title", layout, finalize=True)

while True:
    event, values = window.read()
    if event == sg.WINDOW_CLOSED:
        break
    elif event == 'President':
        index = values[event][0]
        window['Birthday'].update(value=f'Birthday: {data[index][1]}')

window.close()

Upvotes: 2

Related Questions