How to import values from a window to another window

I have put a solution (PySimpleGUI: Working with Multiple Windows - Mouse Vs Python) into a program to make a save function, but everytime I tried to use an input for a value into said program, it returns a “Key Error” error. I print the values, but found no value relating to the input. Here’s the example code; run the program and click on the “print” key if you don’t believe me.

import PySimpleGUI as sg

def main():
    layout = [  [sg.Button(button_text = 'Save', key = 'SV'), sg.Button(button_text = 'Print', key = 'PR')],
                [sg.Input(default_text = "0", key = "V1")]  ]
    window = sg.Window("Main", layout)
    while True:
        event, values = window.read()
        if event == "Exit" or event == sg.WIN_CLOSED:
            break

        value1 = values['V1']

        if event == 'PR':
            print(event, values)

        if event == 'SV':
            if sg.Window("Other Window", [[sg.Input(default_text = "0", key = "V2")], 
                                          [sg.Button(button_text = 'Save', key = 'IN')]]).read(close=True)[0] == 'IN':
                print(values["V2"])
        
    window.close()
if __name__ == "__main__":
    main()

This reads the values from the first window. But values["V2"] is from the second window. You should assign the second window to another variable and read the values from that again before you can print them.

Thanks, but how do you assign a variable to the second window and import it to the main window in a method like this?

This line does many things at once. Let’s change it one step at a time. First extract the window into a variable:

window2  = sg.Window("Other Window", [[sg.Input(default_text = "0", key = "V2")], 
                                          [sg.Button(button_text = 'Save', key = 'IN')]])
if window2.read(close=True)[0] == 'IN':

Now put the read on a separate line:

window2  = sg.Window("Other Window", [[sg.Input(default_text = "0", key = "V2")], 
                                          [sg.Button(button_text = 'Save', key = 'IN')]])
read_result = window2.read(close=True)
if read_result[0] == 'IN':

This still doesn’t work because we didn’t really change anything.

But now the change to fix it is simple:

window2  = sg.Window("Other Window", [[sg.Input(default_text = "0", key = "V2")], 
                                          [sg.Button(button_text = 'Save', key = 'IN')]])
event, values = window2.read(close=True)
if event == 'IN':

Before only the event of the second window was used (without assigning it to a variable). Now both the event and the values are assigned to variables. So when you later use the values variable, they contain the data from this second window.

Thanks. It should look like this:

import PySimpleGUI as sg

def main():
    layout = [  [sg.Button(button_text = 'Save', key = 'SV'), sg.Button(button_text = 'Print', key = 'PR')],
                [sg.Input(default_text = "0", key = "V1")]  ]
    window = sg.Window("Main", layout)
    while True:
        event, values = window.read()
        if event == "Exit" or event == sg.WIN_CLOSED:
            break

        value1 = values['V1']

        if event == 'PR':
            print(event, values)

        if event == 'SV':
            window2  = sg.Window("Other Window", [[sg.Input(default_text = "0", key = "V2")], 
                                          [sg.Button(button_text = 'Save', key = 'IN')]])
            event, values = window2.read(close=True)
            if event == 'IN':
                print(values["V2"])
        
    window.close()
if __name__ == "__main__":
    main()