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

@ -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()