From f0b590098ef54129ae26ed9383fe846874188a90 Mon Sep 17 00:00:00 2001 From: PySimpleGUI Date: Fri, 7 Aug 2020 07:32:28 -0400 Subject: [PATCH 1/3] Added ablity to re-open window 2 if closed. Also made windows use input events so updates are made as soon as characters entered --- ...n_Pattern_Multiple_Windows_Both_Visible.py | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/DemoPrograms/Demo_Design_Pattern_Multiple_Windows_Both_Visible.py b/DemoPrograms/Demo_Design_Pattern_Multiple_Windows_Both_Visible.py index c0f441cb..2b070724 100644 --- a/DemoPrograms/Demo_Design_Pattern_Multiple_Windows_Both_Visible.py +++ b/DemoPrograms/Demo_Design_Pattern_Multiple_Windows_Both_Visible.py @@ -3,6 +3,9 @@ import PySimpleGUI as sg Demo - 2 simultaneous windows using read_all_window 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 """ @@ -10,28 +13,30 @@ import PySimpleGUI as sg def make_win1(): layout = [[sg.Text('Window 1')], [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.Button('Output to Window 2'), sg.Button('Exit')]] + [sg.Button('Reopen')], + [sg.Button('Exit')]] return sg.Window('Window Title', layout, finalize=True) def make_win2(): layout = [[sg.Text('Window 2')], [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.Button('Output to Window 1'), sg.Button('Exit')]] + [sg.Button('Exit')]] return sg.Window('Window Title', layout, finalize=True) def main(): 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 window, event, values = sg.read_all_windows() + if window == sg.WIN_CLOSED: # if all windows were closed break if event == sg.WIN_CLOSED or event == 'Exit': @@ -40,7 +45,11 @@ def main(): window2 = None elif window == window1: # if closing win 1, exit program 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 if output_window: # if a valid window, then output to it output_window['-OUTPUT-'].update(values['-IN-']) From e785dc79a49a0ab9320319c6c9fb50b048564111 Mon Sep 17 00:00:00 2001 From: PySimpleGUI Date: Fri, 7 Aug 2020 07:34:51 -0400 Subject: [PATCH 2/3] Fixed comment about checking for window closed --- .../Demo_Design_Pattern_Multiple_Windows_Both_Visible.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/DemoPrograms/Demo_Design_Pattern_Multiple_Windows_Both_Visible.py b/DemoPrograms/Demo_Design_Pattern_Multiple_Windows_Both_Visible.py index 2b070724..c85cb399 100644 --- a/DemoPrograms/Demo_Design_Pattern_Multiple_Windows_Both_Visible.py +++ b/DemoPrograms/Demo_Design_Pattern_Multiple_Windows_Both_Visible.py @@ -6,6 +6,8 @@ import PySimpleGUI as sg 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. + + The program exits when both windows have been closed Copyright 2020 PySimpleGUI.org """ @@ -43,7 +45,7 @@ def main(): window.close() if window == window2: # if closing win 2, mark as closed window2 = None - elif window == window1: # if closing win 1, exit program + elif window == window1: # if closing win 1, mark as closed window1 = None elif event == 'Reopen': if not window2: From 63cd2e66598e7fa08999aa72bb36702088bffbce Mon Sep 17 00:00:00 2001 From: PySimpleGUI Date: Fri, 7 Aug 2020 07:35:14 -0400 Subject: [PATCH 3/3] New read_all_windows Recipe --- docs/cookbook.md | 79 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/docs/cookbook.md b/docs/cookbook.md index 98681c2e..03ef3bde 100644 --- a/docs/cookbook.md +++ b/docs/cookbook.md @@ -2871,6 +2871,85 @@ while True: # Event Loop window.close() ``` +--------------------- + +# Recipe Multiple Windows - `read_all_windows` + +Beginning in version 4.28.0 you'll find that working with multiple windows in the tkinter port of PySimpleGUI to be much much easier. + +This Recipe shows 2 windows. Both of them are active and can be interacted with. When you enter something in window 1 it is updated in window 2. Notice that the keys are named the same in both windows. This makes it really easy to write generic code that will update fields in either window, the only difference will be which Window is updated. + +You'll find that you'll have less chances for problems like "reusing layouts" if you put your layout and window creation into a function. This will guarantee a "fresh" window every time you call the function. If you close window 2 and then click the "Reopen" button in window 1, then all that is needed is to call the `make_win2` function again and move the new window to the location below the first window. + +The program remains active until both windows have been closed. + + +```python +import PySimpleGUI as sg +""" + Demo - 2 simultaneous windows using read_all_window + + Both windows are immediately visible. Each window updates the other. + + Copyright 2020 PySimpleGUI.org +""" + +def make_win1(): + layout = [[sg.Text('Window 1')], + [sg.Text('Enter something to output to Window 2')], + [sg.Input(key='-IN-', enable_events=True)], + [sg.Text(size=(25,1), key='-OUTPUT-')], + [sg.Button('Reopen')], + [sg.Button('Exit')]] + return sg.Window('Window Title', layout, finalize=True) + + +def make_win2(): + layout = [[sg.Text('Window 2')], + [sg.Text('Enter something to output to Window 1')], + [sg.Input(key='-IN-', enable_events=True)], + [sg.Text(size=(25,1), key='-OUTPUT-')], + [sg.Button('Exit')]] + return sg.Window('Window Title', layout, finalize=True) + + +def main(): + window1, window2 = make_win1(), make_win2() + + window2.move(window1.current_location()[0], window1.current_location()[1]+220) + + while True: # Event Loop + window, event, values = sg.read_all_windows() + + if window == sg.WIN_CLOSED: # if all windows were closed + break + if event == sg.WIN_CLOSED or event == 'Exit': + window.close() + if window == window2: # if closing win 2, mark as closed + window2 = None + elif window == window1: # if closing win 1, mark as closed + window1 = None + 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 + if output_window: # if a valid window, then output to it + output_window['-OUTPUT-'].update(values['-IN-']) + else: + window['-OUTPUT-'].update('Other window is closed') + + +if __name__ == '__main__': + main() + ``` + + + +-------------------- + + ## Multiple Windows