Gidil
Gidil

Reputation: 4137

Reacting to a users choice in a combo-box (Event)

I have a working form in PySimpleGUI with a combo-box. I would like to update combo-box "y" (set the default of this combo-box) when the user selects a value in combo-box "x".

I think that I should capture the event and update the "y" element's value, in order to set the "default", but I haven't been able to figure out how to capture this event. I haven't found any good examples of this use case either.

Specifically, after the user chooses a 'Name' from the first combo-box, I would like to update the default value in the 'Status' combo-box:

import gspread
import pandas as pd
import PySimpleGUI as sg
.
.
.
lst = records_df.Label.to_list()

Status = ['A','B','C','D']

layout = [
        [sg.Text('Please Choose:',size=(39,1))],
        [sg.Combo(lst, key='combo', size=(20,1)),sg.Text('Name:', size=(18,1))],
        [sg.Combo(Status, key='comboStatus', size=(20,1)),sg.Text('Status:', size=(18,1))],
        [ sg.Button('Exit', key='Exit', size=(18,1)), sg.Button('Submit', key='Submit', size=(20,1))]
]
.
.
.
while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED or event == 'Exit':
        break
    elif event == 'Submit':

window.close()

Upvotes: 1

Views: 3195

Answers (1)

thisismy-stackoverflow
thisismy-stackoverflow

Reputation: 468

It's easier than you'd think. Just add enable_events=True to the combo-box, and it will pass an event every time an item is chosen.

layout = [
    [sg.Text('Please Choose:',size=(39, 1))],
    [sg.Combo(lst, key='combo', size=(20, 1), enable_events=True), sg.Text('Name:', size=(18, 1))],
    [sg.Combo(Status, key='comboStatus', size=(20, 1)),sg.Text('Status:', size=(18, 1))],
    [sg.Button('Exit', key='Exit', size=(18, 1)), sg.Button('Submit', key='Submit', size=(20, 1))]
]

while True:
    event, values = window.read()
    if event == 'combo':    # the event's name is the element's key
        print(values)

I couldn't, however, get it to pass an event when you manually enter text into the combobox, even after pressing enter. Hopefully that isn't an issue for your use-case.

Upvotes: 5

Related Questions