pythonuser-interfacepysimplegui

Is there a way to have the window automatically resize once an element is hidden in the layout?


I have two radio buttons. When the .csv button is selected, I want there to only be one input box. when .rsp is selected, I have a new input box appear. It creates space to fit between the radio buttons and the create button. When it disappears, the space remains. Is there a way to get rid of this space when the element is hidden?

import PySimpleGUI as sg

# Define a custom theme for the GUI
my_new_theme = {'BACKGROUND': "#31363b",
                'TEXT': "#f9f1ee",
                'INPUT': "#232629",
                'TEXT_INPUT': "#f9f1ee",
                'SCROLL': "#333a41",
                'BUTTON': ('#31363b', '#0dd1fc'),
                'PROGRESS': ('#f9f1ee', '#31363b'),
                'BORDER': 1,
                'SLIDER_DEPTH': 0,
                'PROGRESS_DEPTH': 0}

# Add and set the custom theme
sg.theme_add_new("MyNewTheme", my_new_theme)
sg.theme("MyNewTheme")
sg.set_options(font=("Helvetica", 12))



layout = [
    [sg.Text("Input mesh name:", pad=(1, 5)), sg.InputText(key="mesh")],

    [sg.Text("Paste boundary conditions below:", pad=(1, 5))],
    [sg.Multiline(key="bound_cond", size=(35, 10), pad=(1, 5))],

    [sg.Radio(".csv", "RADIO1", default=True, key='-CSV-', enable_events=True, pad=(1, 5)),
     sg.Radio(".rsp", "RADIO1", key='-RSP-', enable_events=True, pad=(1, 5))],

    [sg.Text("Select .csv or .rsp file:", size=(17, 1)), sg.Input(), sg.FileBrowse("Choose")],

    [sg.Column([[sg.Text("Auto-assign:", size=(17, 1)), sg.Input(key='-FILE-'),
                 sg.FileBrowse("Choose")]], key='-NODE_FILE-', visible=False, pad=(0, 0))],
    [sg.Button("Create")]
]

window = sg.Window("Input Creation Wizard", layout=layout)

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

    match event:
        case sg.WIN_CLOSED:
            break

        case '-RSP-':
            window['-NODE_FILE-'].update(visible=True)

        case '-CSV-':
            window['-NODE_FILE-'].update(visible=False)

window.close()

Solution

  • Pin's an element provided into a layout so that when it's made invisible and visible again, it will be in the correct place. Otherwise it will be placed at the end of its containing window/column.

    The element you want to pin is the element that you'll be making visibile/invisible.

    The pin helper function also causes containers to shrink to fit the contents correct after something inside has changed visiblity. Note that setting a hardcoded size on your window can impact this ability to shrink.

    To remove the space occupied by a element, you can do it by setting the option vaisible=False, also use the pin helper function to cause containers to shrink to fit the contents correct after something inside has changed visiblity .

        [sg.pin(sg.Column([[sg.Text("Auto-assign:", size=(17, 1)), sg.Input(key='-FILE-'),
                     sg.FileBrowse("Choose")]], key='-NODE_FILE-', visible=False, pad=(0, 0)))],