All multi-window design patterns updated to use the new read_all_windows() call.
This commit is contained in:
parent
102e61cdab
commit
b8289c7360
|
@ -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()
|
|
@ -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
|
||||
|
||||
def make_window1():
|
||||
layout = [[sg.Text('Window 1'), ],
|
||||
[sg.Input()],
|
||||
[sg.Text('', size=(20,1), key='-OUTPUT-')],
|
||||
[sg.Button('Launch 2')]]
|
||||
[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])
|
||||
|
||||
if event1 == 'Launch 2' and not window2_active:
|
||||
window2_active = True
|
||||
window1.hide()
|
||||
layout2 = [[sg.Text('Window 2')],
|
||||
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)
|
||||
|
||||
|
||||
def main():
|
||||
# Design pattern 1 - First window does not remain active
|
||||
window2 = None
|
||||
window1 = make_window1()
|
||||
|
||||
while True:
|
||||
ev2, vals2 = window2.read()
|
||||
if ev2 is None or ev2 == 'Exit':
|
||||
window2.close()
|
||||
window2_active = False
|
||||
window1.un_hide()
|
||||
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()
|
|
@ -1,38 +1,52 @@
|
|||
import PySimpleGUI as sg
|
||||
|
||||
"""
|
||||
PySimpleGUI The Complete Course
|
||||
Lesson 7
|
||||
Multiple Independent Windows
|
||||
"""
|
||||
# Design pattern 2 - First window remains active
|
||||
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
|
||||
|
||||
"""
|
||||
|
||||
|
||||
def make_window1():
|
||||
layout = [[ sg.Text('Window 1'),],
|
||||
[sg.Input()],
|
||||
[sg.Text('', size=(20,1), key='-OUTPUT-')],
|
||||
[sg.Input(enable_events=True, k='-IN-')],
|
||||
[sg.Text(size=(20,1), k='-OUTPUT-')],
|
||||
[sg.Button('Launch 2'), sg.Button('Exit')]]
|
||||
|
||||
window1 = sg.Window('Window 1', layout)
|
||||
return sg.Window('Window 1', layout, finalize=True)
|
||||
|
||||
window2_active = False
|
||||
|
||||
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')],
|
||||
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()
|
||||
window2.close()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
|
@ -4,51 +4,52 @@ import PySimpleGUI as sg
|
|||
Example of wizard-like PySimpleGUI windows
|
||||
'''
|
||||
|
||||
def make_window1():
|
||||
layout = [[sg.Text('Window 1'), ],
|
||||
[sg.Input()],
|
||||
[sg.Text('',size=(20,1), key='-OUTPUT-')],
|
||||
[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)
|
||||
|
||||
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])
|
||||
|
||||
if not window2_active and event1 == 'Next >':
|
||||
window2_active = True
|
||||
window.hide()
|
||||
layout2 = [[sg.Text('Window 2')],
|
||||
def make_window2():
|
||||
layout = [[sg.Text('Window 2')],
|
||||
[sg.Button('< Prev'), sg.Button('Next >')]]
|
||||
|
||||
window2 = sg.Window('Window 2', layout2)
|
||||
return sg.Window('Window 2', layout, finalize=True)
|
||||
|
||||
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()
|
||||
layout3 = [[sg.Text('Window 3')],
|
||||
|
||||
def make_window3():
|
||||
layout = [[sg.Text('Window 3')],
|
||||
[sg.Button('< Prev'), sg.Button('Exit')]]
|
||||
window3 = sg.Window('Window 3', layout3)
|
||||
return sg.Window('Window 3', layout, finalize=True)
|
||||
|
||||
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'):
|
||||
|
||||
|
||||
window1, window2, window3 = make_window1(), None, None
|
||||
|
||||
while True:
|
||||
window, event, values = sg.read_all_windows()
|
||||
if window == window1 and event in (sg.WIN_CLOSED, 'Exit'):
|
||||
break
|
||||
|
||||
if window == window1:
|
||||
if event == 'Next >':
|
||||
window1.hide()
|
||||
window2 = make_window2()
|
||||
window1['-OUTPUT-'].update(values['-IN-'])
|
||||
|
||||
if window == window2:
|
||||
if event == 'Next >':
|
||||
window2.hide()
|
||||
window3 = make_window3()
|
||||
elif event in (sg.WIN_CLOSED, '< Prev'):
|
||||
window2.close()
|
||||
window1.un_hide()
|
||||
|
||||
if window == window3:
|
||||
window3.close()
|
||||
window2.un_hide()
|
||||
|
||||
window.close()
|
||||
|
|
Loading…
Reference in New Issue