[Python] Changing pysimplegui.Combo displayed value from code, not user

Discussão em 'Python' iniciado por Stack, Junho 14, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to create a drop-down list that whose value is saved at the press of a button (my actual application uses multiple drop-downs and a single Apply). I also want a Reset button to discard changes made since last hitting Apply. How can I go back and edit the displayed value of sg.Combo?

    Example Code:

    import PySimpleGUI27 as sg # NOTE: If using python3, delete '27'

    if __name__ == "__main__":
    data = '' # Stored data
    layout = [
    [sg.Combo(['', 'opt1', 'opt2', 'opt3'], key='_key_'), sg.Button('Apply')],
    [sg.Button('Reset'), sg.Button('Exit')]
    ]
    window = sg.Window('Minimum Example', layout)

    while True:
    event, value = window.read()
    if event == 'Exit':
    break
    elif event == 'Apply': # set 'data' to the sg.Combo value
    data = value['_key_']
    elif event == 'Reset': # set sg.Combo value to 'data'
    # This is where my question is focused.
    # How can I change the displayed drop-down back to
    # whatever is saved in 'data'?
    window['_key_'].Update(value=data) # <---- Answer
    if value is None:
    break # Value is None, window is probably closed

    window.close()

    Continue reading...

Compartilhe esta Página