import PySimpleGUI as sg
# 创建窗口1
def create_window1():
layout = [[sg.Text('Window 1')],
[sg.Input(key='-INPUT-')],
[sg.Button('Next')]]
return sg.Window('Window 1', layout, finalize=True)
# 创建窗口2
def create_window2(data):
layout = [[sg.Text('Window 2')],
[sg.Text(f'Input from Window 1: {data}')],
[sg.Button('Close')]]
return sg.Window('Window 2', layout, finalize=True)
def main():
window1 = create_window1()
window2 = None
while True:
window, event, values = sg.read_all_windows()
# 这里是重点,如果当前窗口是窗口1,并且是关闭事件。则需要从values中获取窗口1中的-INPUT-中的值,再关闭窗口1.
if window == window1 and event == sg.WINDOW_CLOSED:
# 获取窗口1的输入值
input_value = values['-INPUT-']
window1.close() # 关闭窗口1
# 创建窗口2,并传递窗口1的输入值
window2 = create_window2(input_value)
if window == window2 and event == sg.WINDOW_CLOSED or event == 'Close':
window2.close() # 关闭窗口2
break
window.close()
if __name__ == '__main__':
main()