From b8289c7360e0b2868a8d5cc738ea4927fb52a207 Mon Sep 17 00:00:00 2001 From: PySimpleGUI Date: Thu, 30 Jul 2020 08:13:54 -0400 Subject: [PATCH] All multi-window design patterns updated to use the new read_all_windows() call. --- .../Demo_Design_Pattern_Multiple_Windows.py | 11 ++- .../Demo_Design_Pattern_Multiple_Windows1.py | 76 +++++++++++------- .../Demo_Design_Pattern_Multiple_Windows2.py | 64 +++++++++------ .../Demo_Design_Pattern_Multiple_Windows3.py | 79 ++++++++++--------- 4 files changed, 133 insertions(+), 97 deletions(-) diff --git a/DemoPrograms/Demo_Design_Pattern_Multiple_Windows.py b/DemoPrograms/Demo_Design_Pattern_Multiple_Windows.py index 9727b759..6c8e9def 100644 --- a/DemoPrograms/Demo_Design_Pattern_Multiple_Windows.py +++ b/DemoPrograms/Demo_Design_Pattern_Multiple_Windows.py @@ -12,16 +12,16 @@ import PySimpleGUI as sg def make_win1(): layout = [[sg.Text('This is the FIRST WINDOW'), sg.Text(' ', k='-OUTPUT-')], - [sg.Text()], + [sg.Text('Click Popup anytime to see a modal popup')], [sg.Button('Launch 2nd Window'), sg.Button('Popup'), sg.Button('Exit')]] return sg.Window('Window Title', layout, location=(800,600), finalize=True) def make_win2(): layout = [[sg.Text('The second window')], - [sg.Input(key='-IN-')], + [sg.Input(key='-IN-', enable_events=True)], [sg.Text(size=(25,1), k='-OUTPUT-')], - [sg.Button('Show'), sg.Button('Popup'), sg.Button('Exit')]] + [sg.Button('Erase'), sg.Button('Popup'), sg.Button('Exit')]] return sg.Window('Second Window', layout, finalize=True) window1, window2 = make_win1(), None # start off with 1 window open @@ -38,6 +38,9 @@ while True: # Event Loop sg.popup('This is a BLOCKING popup','all windows remain inactive while popup active') elif event == 'Launch 2nd Window' and not window2: window2 = make_win2() - elif event == 'Show': + elif event == '-IN-': window['-OUTPUT-'].update(f'You enetered {values["-IN-"]}') + elif event == 'Erase': + window['-OUTPUT-'].update('') + window['-IN-'].update('') window.close() \ No newline at end of file diff --git a/DemoPrograms/Demo_Design_Pattern_Multiple_Windows1.py b/DemoPrograms/Demo_Design_Pattern_Multiple_Windows1.py index 030d2d9e..8c7aec14 100644 --- a/DemoPrograms/Demo_Design_Pattern_Multiple_Windows1.py +++ b/DemoPrograms/Demo_Design_Pattern_Multiple_Windows1.py @@ -1,39 +1,57 @@ import PySimpleGUI as sg + """ - PySimpleGUI The Complete Course - Lesson 7 - Multiple Windows - 1-lvl nested window + Design pattern multiple windows + Using read_all_windows() + + Only 1 window at a time is visible/active on the screen. + + Window1 opens Window2 + When Window2 closes, Window1 reappears + Program exits when Window1 is closed + + Copyright 2020 PySimpleGUI.org """ -# Design pattern 1 - First window does not remain active -layout = [[ sg.Text('Window 1'),], - [sg.Input()], - [sg.Text('', size=(20,1), key='-OUTPUT-')], - [sg.Button('Launch 2')]] +def make_window1(): + layout = [[sg.Text('Window 1'), ], + [sg.Input(key='-IN-')], + [sg.Text(size=(20, 1), key='-OUTPUT-')], + [sg.Button('Launch 2'), sg.Button('Output')]] -window1 = sg.Window('Window 1', layout) -window2_active=False + return sg.Window('Window 1', layout, finalize=True) -while True: - event1, values1 = window1.read(timeout=100) - if event1 is None: - break - window1['-OUTPUT-'].update(values1[0]) +def make_window2(): + layout = [[sg.Text('Window 2')], + [sg.Button('Exit')]] - if event1 == 'Launch 2' and not window2_active: - window2_active = True - window1.hide() - layout2 = [[sg.Text('Window 2')], - [sg.Button('Exit')]] + return sg.Window('Window 2', layout, finalize=True) - window2 = sg.Window('Window 2', layout2) - while True: - ev2, vals2 = window2.read() - if ev2 is None or ev2 == 'Exit': - window2.close() - window2_active = False - window1.un_hide() - break -window1.close() +def main(): + # Design pattern 1 - First window does not remain active + window2 = None + window1 = make_window1() + + while True: + window, event, values = sg.read_all_windows() + if event == sg.WIN_CLOSED and window == window1: + break + + if window == window1: + window1['-OUTPUT-'].update(values['-IN-']) + + if event == 'Launch 2' and not window2: + window1.hide() + window2 = make_window2() + + if window == window2 and (event in (sg.WIN_CLOSED, 'Exit')): + window2.close() + window2 = None + window1.un_hide() + window1.close() + + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/DemoPrograms/Demo_Design_Pattern_Multiple_Windows2.py b/DemoPrograms/Demo_Design_Pattern_Multiple_Windows2.py index 277dc18f..bd4640dd 100644 --- a/DemoPrograms/Demo_Design_Pattern_Multiple_Windows2.py +++ b/DemoPrograms/Demo_Design_Pattern_Multiple_Windows2.py @@ -1,38 +1,52 @@ import PySimpleGUI as sg """ - PySimpleGUI The Complete Course - Lesson 7 - Multiple Independent Windows + Multiple Window Design Pattern + + Two windows - both remain active + Window 1 launches Window 2 + Window 1 remains visible and active while Window 2 is active + Closing Window 1 exits application + """ -# Design pattern 2 - First window remains active -layout = [[ sg.Text('Window 1'),], - [sg.Input()], - [sg.Text('', size=(20,1), key='-OUTPUT-')], - [sg.Button('Launch 2'), sg.Button('Exit')]] -window1 = sg.Window('Window 1', layout) +def make_window1(): + layout = [[ sg.Text('Window 1'),], + [sg.Input(enable_events=True, k='-IN-')], + [sg.Text(size=(20,1), k='-OUTPUT-')], + [sg.Button('Launch 2'), sg.Button('Exit')]] -window2_active = False + return sg.Window('Window 1', layout, finalize=True) -while True: - event1, values1 = window1.read(timeout=100) - window1['-OUTPUT-'].update(values1[0]) - if event1 is None or event1 == 'Exit': - break - if not window2_active and event1 == 'Launch 2': - window2_active = True - layout2 = [[sg.Text('Window 2')], - [sg.Button('Exit')]] +def make_window2(): + layout = [[sg.Text('Window 2')], + [sg.Button('Exit')]] - window2 = sg.Window('Window 2', layout2) + return sg.Window('Window 2', layout, finalize=True) - if window2_active: - ev2, vals2 = window2.read(timeout=100) - if ev2 is None or ev2 == 'Exit': - window2_active = False + +def main(): + window1, window2 = make_window1(), None + while True: + window, event, values = sg.read_all_windows() + if window == window1 and event in (sg.WIN_CLOSED, 'Exit'): + break + # Window 1 stuff + if event == '-IN-': + window['-OUTPUT-'].update(values['-IN-']) + elif event == 'Launch 2' and not window2: + window2 = make_window2() + + # Window 2 stuff + if window == window2 and event in(sg.WIN_CLOSED, 'Exit'): window2.close() + window2 = None -window1.close() + window1.close() + window2.close() + + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/DemoPrograms/Demo_Design_Pattern_Multiple_Windows3.py b/DemoPrograms/Demo_Design_Pattern_Multiple_Windows3.py index 58ce5f10..a2b9785c 100644 --- a/DemoPrograms/Demo_Design_Pattern_Multiple_Windows3.py +++ b/DemoPrograms/Demo_Design_Pattern_Multiple_Windows3.py @@ -4,51 +4,52 @@ import PySimpleGUI as sg Example of wizard-like PySimpleGUI windows ''' -layout = [[sg.Text('Window 1'), ], - [sg.Input()], - [sg.Text('',size=(20,1), key='-OUTPUT-')], - [sg.Button('Next >'), sg.Button('Exit')]] +def make_window1(): + layout = [[sg.Text('Window 1'), ], + [sg.Input(k='-IN-', enable_events=True)], + [sg.Text(size=(20,1), k='-OUTPUT-')], + [sg.Button('Next >'), sg.Button('Exit')]] -window = sg.Window('Window 1', layout) + return sg.Window('Window 1', layout, finalize=True) + + +def make_window2(): + layout = [[sg.Text('Window 2')], + [sg.Button('< Prev'), sg.Button('Next >')]] + + return sg.Window('Window 2', layout, finalize=True) + + +def make_window3(): + layout = [[sg.Text('Window 3')], + [sg.Button('< Prev'), sg.Button('Exit')]] + return sg.Window('Window 3', layout, finalize=True) + + + +window1, window2, window3 = make_window1(), None, None -window3_active = window2_active = False while True: - if not window2_active: - event1, values1 = window.read() - if event1 is None or event1 == 'Exit': - break - window['-OUTPUT-'].update(values1[0]) + window, event, values = sg.read_all_windows() + if window == window1 and event in (sg.WIN_CLOSED, 'Exit'): + break - if not window2_active and event1 == 'Next >': - window2_active = True - window.hide() - layout2 = [[sg.Text('Window 2')], - [sg.Button('< Prev'), sg.Button('Next >')]] + if window == window1: + if event == 'Next >': + window1.hide() + window2 = make_window2() + window1['-OUTPUT-'].update(values['-IN-']) - window2 = sg.Window('Window 2', layout2) - - if window2_active: - event2 = window2.read()[0] - if event2 in (sg.WIN_CLOSED, 'Exit', '< Prev'): - window2_active = False - window2.close() - window.un_hide() - elif event2 == 'Next >': - window3_active = True - window2_active = False + if window == window2: + if event == 'Next >': window2.hide() - layout3 = [[sg.Text('Window 3')], - [sg.Button('< Prev'), sg.Button('Exit')]] - window3 = sg.Window('Window 3', layout3) + window3 = make_window3() + elif event in (sg.WIN_CLOSED, '< Prev'): + window2.close() + window1.un_hide() - if window3_active: - ev3, vals3 = window3.read() - if ev3 == '< Prev': - window3.close() - window3_active = False - window2_active = True - window2.un_hide() - elif ev3 in (sg.WIN_CLOSED, 'Exit'): - break + if window == window3: + window3.close() + window2.un_hide() window.close()