Search code examples
pythontkinterpysimplegui

Add new input boxes on PySimpleGUI when updated


I have a dictionary with information :

Facets = {
    'Entity'        : {'facet_type': 'Entity'        , 'facet_parameters': ['IFC Class'   , 'Predefined Type']},
    'Attribute'     : {'facet_type': 'Attribute'     , 'facet_parameters': ['Name'        , 'Value']},
    'Classification': {'facet_type': 'Classification', 'facet_parameters': ['System'      , 'Value']},
    'Property'      : {'facet_type': 'Property'      , 'facet_parameters': ['Property Set', 'Name', 'Value']},
    'Material'      : {'facet_type': 'Material'      , 'facet_parameters': ['Value']},
    'Parts'         : {'facet_type': 'Parts'         , 'facet_parameters': ['Entity'      , 'Relationship']}
}

I want to create an (IDS) editor using pySimpleGUI. People should select the facet_type and then be able to enter the facet_parameters themself.

so in layout the following allow them to select the correct facet_type :

layout = [[sg.Text('All elements of'), 
           sg.Combo(sorted(Facets.keys()),key='-FACET-', enable_events=True)]]

And then in the while loop I want that for each parameter in the facet parameters, it gives an imput box but I cant do it

window = sg.Window('IDS', layout,resizable=True)

while True:
    event, values = window.read()
    
    if event == sg.WINDOW_CLOSED:
        break
    elif event == '-FACET-':
        selected_facet = values['-FACET-']
        facet_parameters = Facets[selected_facet]['facet_parameters']

So far I am stuck here and can't produce any piece of code that would update the layout to add something like that (screenshot to explain) :

Screenshot showing the desired result

Thank you for your time.


Solution

  • If the values for dict Facets are fixed, you can build the layout for all elements when initial, just set only which one visible.

    import PySimpleGUI as sg
    
    def column_layout(category):
        items = Facets[category]['facet_parameters']
        layout = [[sg.Text(item), sg.Push(), sg.Input(key=(category, item))] for item in items]
        return layout
    
    Facets = {
        'Entity'        : {'facet_type': 'Entity'        , 'facet_parameters': ['IFC Class'   , 'Predefined Type']},
        'Attribute'     : {'facet_type': 'Attribute'     , 'facet_parameters': ['Name'        , 'Value']},
        'Classification': {'facet_type': 'Classification', 'facet_parameters': ['System'      , 'Value']},
        'Property'      : {'facet_type': 'Property'      , 'facet_parameters': ['Property Set', 'Name', 'Value']},
        'Material'      : {'facet_type': 'Material'      , 'facet_parameters': ['Value']},
        'Parts'         : {'facet_type': 'Parts'         , 'facet_parameters': ['Entity'      , 'Relationship']}
    }
    
    categories = sorted(Facets.keys())
    category = categories[0]
    
    sg.set_options(font=("Courier New", 12))
    sub_column = [[sg.Column(column_layout(category), visible=(i==0), key=category) for i, category in enumerate(categories)]]
    layout = [
        [sg.Push(),
         sg.Text('All elements of'),
         sg.Combo(categories, default_value=category, enable_events=True, key='-FACET-'),
         sg.Push()],
        [sg.Column(sub_column)],
    ]
    window = sg.Window('Title', layout)
    
    while True:
    
        event, values = window.read()
    
        if event == sg.WIN_CLOSED:
            break
        elif event == '-FACET-':
            window[category].update(visible=False)
            category = values[event]
            window[category].update(visible=True)
            window.refresh()
            window.move_to_center()
    
    window.close()
    

    enter image description here