I have created a PySimpleGUI combo box with code which simplifies down to:
combo_list = ['Choice 1', 'Choice 2']
layout = [[sg.Combo(combo_list, default_value=combo_list[0], key='-COMBO-', enable_events=True)]]
The user is able to add items to the drop-down list, and when he quits the app, I save the updated combo_list so that it can be used when the program is next run. At the moment I am doing this by adding any new entries made by the user, to my combo_list array. But is there a way to simply read back the updated parameter 'values' from the Combo element itself rather than having to shadow it by updating the combo_list array?
There is no problem reading back the value that the user has chosen using window['-COMBO-'].get()
, but in spite of checking the PySimpleGUI docs, and more generally, I have not been able to find a way to read the list of values. If this is not possible, I will continue to update the combo_list array whenever the user adds an item. But it would be good to know.
get
Returns the current (right now) value of the Combo. DO NOT USE THIS AS THE NORMAL WAY OF READING A COMBO! You should be using values from your call to window.read instead. Know what you're doing if you use it.
To get the combo_list
of a Combo element, you can get it by tkinter code, like window['-COMBO-'].widget['values']
.
import PySimpleGUI as sg
combo_list = ['Choice 1', 'Choice 2']
layout = [[sg.Combo(combo_list, default_value=combo_list[0], key='-COMBO-', enable_events=True)]]
window = sg.Window("Title", layout)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
print(event, values)
print(window['-COMBO-'].widget['values'])
window.close()
-COMBO- {'-COMBO-': 'Choice 2'}
('Choice 1', 'Choice 2')