All multi-window design patterns updated to use the new read_all_windows() call.

This commit is contained in:
PySimpleGUI 2020-07-30 08:13:54 -04:00
parent 102e61cdab
commit b8289c7360
4 changed files with 133 additions and 97 deletions

View File

@ -12,16 +12,16 @@ import PySimpleGUI as sg
def make_win1(): def make_win1():
layout = [[sg.Text('This is the FIRST WINDOW'), sg.Text(' ', k='-OUTPUT-')], 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')]] [sg.Button('Launch 2nd Window'), sg.Button('Popup'), sg.Button('Exit')]]
return sg.Window('Window Title', layout, location=(800,600), finalize=True) return sg.Window('Window Title', layout, location=(800,600), finalize=True)
def make_win2(): def make_win2():
layout = [[sg.Text('The second window')], 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.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) return sg.Window('Second Window', layout, finalize=True)
window1, window2 = make_win1(), None # start off with 1 window open 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') sg.popup('This is a BLOCKING popup','all windows remain inactive while popup active')
elif event == 'Launch 2nd Window' and not window2: elif event == 'Launch 2nd Window' and not window2:
window2 = make_win2() window2 = make_win2()
elif event == 'Show': elif event == '-IN-':
window['-OUTPUT-'].update(f'You enetered {values["-IN-"]}') window['-OUTPUT-'].update(f'You enetered {values["-IN-"]}')
elif event == 'Erase':
window['-OUTPUT-'].update('')
window['-IN-'].update('')
window.close() window.close()

View File

@ -1,39 +1,57 @@
import PySimpleGUI as sg import PySimpleGUI as sg
""" """
PySimpleGUI The Complete Course Design pattern multiple windows
Lesson 7 - Multiple Windows Using read_all_windows()
1-lvl nested window
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'),], def make_window1():
[sg.Input()], layout = [[sg.Text('Window 1'), ],
[sg.Text('', size=(20,1), key='-OUTPUT-')], [sg.Input(key='-IN-')],
[sg.Button('Launch 2')]] [sg.Text(size=(20, 1), key='-OUTPUT-')],
[sg.Button('Launch 2'), sg.Button('Output')]]
window1 = sg.Window('Window 1', layout) return sg.Window('Window 1', layout, finalize=True)
window2_active=False
while True: def make_window2():
event1, values1 = window1.read(timeout=100) layout = [[sg.Text('Window 2')],
if event1 is None: [sg.Button('Exit')]]
break
window1['-OUTPUT-'].update(values1[0])
if event1 == 'Launch 2' and not window2_active: return sg.Window('Window 2', layout, finalize=True)
window2_active = True
window1.hide()
layout2 = [[sg.Text('Window 2')],
[sg.Button('Exit')]]
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()

View File

@ -1,38 +1,52 @@
import PySimpleGUI as sg import PySimpleGUI as sg
""" """
PySimpleGUI The Complete Course Multiple Window Design Pattern
Lesson 7
Multiple Independent Windows 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': def make_window2():
window2_active = True layout = [[sg.Text('Window 2')],
layout2 = [[sg.Text('Window 2')], [sg.Button('Exit')]]
[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) def main():
if ev2 is None or ev2 == 'Exit': window1, window2 = make_window1(), None
window2_active = False 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.close()
window2 = None
window1.close() window1.close()
window2.close()
if __name__ == '__main__':
main()

View File

@ -4,51 +4,52 @@ import PySimpleGUI as sg
Example of wizard-like PySimpleGUI windows Example of wizard-like PySimpleGUI windows
''' '''
layout = [[sg.Text('Window 1'), ], def make_window1():
[sg.Input()], layout = [[sg.Text('Window 1'), ],
[sg.Text('',size=(20,1), key='-OUTPUT-')], [sg.Input(k='-IN-', enable_events=True)],
[sg.Button('Next >'), sg.Button('Exit')]] [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: while True:
if not window2_active: window, event, values = sg.read_all_windows()
event1, values1 = window.read() if window == window1 and event in (sg.WIN_CLOSED, 'Exit'):
if event1 is None or event1 == 'Exit': break
break
window['-OUTPUT-'].update(values1[0])
if not window2_active and event1 == 'Next >': if window == window1:
window2_active = True if event == 'Next >':
window.hide() window1.hide()
layout2 = [[sg.Text('Window 2')], window2 = make_window2()
[sg.Button('< Prev'), sg.Button('Next >')]] window1['-OUTPUT-'].update(values['-IN-'])
window2 = sg.Window('Window 2', layout2) if window == window2:
if event == 'Next >':
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
window2.hide() window2.hide()
layout3 = [[sg.Text('Window 3')], window3 = make_window3()
[sg.Button('< Prev'), sg.Button('Exit')]] elif event in (sg.WIN_CLOSED, '< Prev'):
window3 = sg.Window('Window 3', layout3) window2.close()
window1.un_hide()
if window3_active: if window == window3:
ev3, vals3 = window3.read() window3.close()
if ev3 == '< Prev': window2.un_hide()
window3.close()
window3_active = False
window2_active = True
window2.un_hide()
elif ev3 in (sg.WIN_CLOSED, 'Exit'):
break
window.close() window.close()