The user can search for names in the database with the search box and select the name they want. a second window will appear for them to enter information. Any interaction with the second window causes a KeyError for values["IN"].
import PySimpleGUI as sg
pt_list = ["Test 1", "Test 2", "Test 3"]
def make_window1():
layout = [
[sg.Button("Add New Patient", key = "ADD"), sg.Button("Remove Patient", key = "RMV")],
[sg.Input("Search patient name", key = "IN", enable_events = True), sg.Button("Search", key = "SEARCH")],
[sg.Listbox(pt_list, expand_x = True, expand_y = True, key = "LIST", enable_events = True)]
]
return sg.Window("WINDOW1", layout, finalize = True)
def make_window2():
layout = [
[sg.Text("A second window has appeared.")],
[sg.Button("Close 2nd window")],
]
return sg.Window("WINDOW2", layout, finalize = True)
def main():
window2 = None
window1 = make_window1()
while True:
window, event, values = sg.read_all_windows()
if event == sg.WIN_CLOSED and window == window1 or event == "Quit":
break
#Patient selcetion list
# if a keystroke entered in search field
if values['IN'] != '':
search = values['IN']
# do the filtering
new_values = [x for x in pt_list if search in x]
# display in the listbox
window['LIST'].update(new_values)
else:
# display original unfiltered list
window['LIST'].update(pt_list)
# if a list item is chosen, switch to patient folder
if event == 'LIST' and len(values['LIST']):
window2 = make_window2()
window1.hide()
if window == window2 and (event in (sg.WIN_CLOSED, 'Save & Close')):
window2.close()
window2 = None
window1.un_hide()
window1.close()
#Running the program
if __name__ == '__main__':
main()
If the second window is not called it works as intended but i cannot think of why calling the second window would cause the KeyError, i dont see the connection.
Traceback (most recent call last):
File "C:\Users\ps2ee\Downloads\Testmulti.py", line 59, in <module>
main()
File "C:\Users\ps2ee\Downloads\Testmulti.py", line 36, in main
if values['IN'] != '':
KeyError: 'IN'
i was able to get it to work with the following.
I wasn't checking for which window object values["IN"] was in. thank you @JasonYang for pointing me in the right direction