Added ablity to re-open window 2 if closed. Also made windows use input events so updates are made as soon as characters entered

This commit is contained in:
PySimpleGUI 2020-08-07 07:32:28 -04:00
parent 54924e017d
commit f0b590098e
1 changed files with 15 additions and 6 deletions

View File

@ -4,34 +4,39 @@ import PySimpleGUI as sg
Both windows are immediately visible. Each window updates the other. Both windows are immediately visible. Each window updates the other.
There's an added capability to "re-open" window 2 should it be closed. This is done by simply calling the make_win2 function
again when the button is pressed in window 1.
Copyright 2020 PySimpleGUI.org Copyright 2020 PySimpleGUI.org
""" """
def make_win1(): def make_win1():
layout = [[sg.Text('Window 1')], layout = [[sg.Text('Window 1')],
[sg.Text('Enter something to output to Window 2')], [sg.Text('Enter something to output to Window 2')],
[sg.Input(key='-IN-')], [sg.Input(key='-IN-', enable_events=True)],
[sg.Text(size=(25,1), key='-OUTPUT-')], [sg.Text(size=(25,1), key='-OUTPUT-')],
[sg.Button('Output to Window 2'), sg.Button('Exit')]] [sg.Button('Reopen')],
[sg.Button('Exit')]]
return sg.Window('Window Title', layout, finalize=True) return sg.Window('Window Title', layout, finalize=True)
def make_win2(): def make_win2():
layout = [[sg.Text('Window 2')], layout = [[sg.Text('Window 2')],
[sg.Text('Enter something to output to Window 1')], [sg.Text('Enter something to output to Window 1')],
[sg.Input(key='-IN-')], [sg.Input(key='-IN-', enable_events=True)],
[sg.Text(size=(25,1), key='-OUTPUT-')], [sg.Text(size=(25,1), key='-OUTPUT-')],
[sg.Button('Output to Window 1'), sg.Button('Exit')]] [sg.Button('Exit')]]
return sg.Window('Window Title', layout, finalize=True) return sg.Window('Window Title', layout, finalize=True)
def main(): def main():
window1, window2 = make_win1(), make_win2() window1, window2 = make_win1(), make_win2()
window2.move(window1.current_location()[0], window1.current_location()[1]+200) window2.move(window1.current_location()[0], window1.current_location()[1]+220)
while True: # Event Loop while True: # Event Loop
window, event, values = sg.read_all_windows() window, event, values = sg.read_all_windows()
if window == sg.WIN_CLOSED: # if all windows were closed if window == sg.WIN_CLOSED: # if all windows were closed
break break
if event == sg.WIN_CLOSED or event == 'Exit': if event == sg.WIN_CLOSED or event == 'Exit':
@ -40,7 +45,11 @@ def main():
window2 = None window2 = None
elif window == window1: # if closing win 1, exit program elif window == window1: # if closing win 1, exit program
window1 = None window1 = None
elif event.startswith('Output to'): elif event == 'Reopen':
if not window2:
window2 = make_win2()
window2.move(window1.current_location()[0], window1.current_location()[1] + 220)
elif event == '-IN-':
output_window = window2 if window == window1 else window1 output_window = window2 if window == window1 else window1
if output_window: # if a valid window, then output to it if output_window: # if a valid window, then output to it
output_window['-OUTPUT-'].update(values['-IN-']) output_window['-OUTPUT-'].update(values['-IN-'])