Replaced one multi-window design pattern with new read_all_windows
This commit is contained in:
parent
a3357df30a
commit
8fc47b964a
|
@ -1,54 +1,43 @@
|
||||||
import PySimpleGUI as sg
|
import PySimpleGUI as sg
|
||||||
"""
|
"""
|
||||||
Demo - Running 2 windows with both being active at the same time
|
Demo - 2 simultaneous windows using read_all_window
|
||||||
Three important things to note about this design patter:
|
|
||||||
1. The layout for window 2 is inside of the while loop, just before the call to window2=sg.Window
|
Window 1 launches window 2
|
||||||
2. The read calls have timeout values of 100 and 0. You can change the 100 to whatever interval you wish
|
BOTH remain active in parallel
|
||||||
but must keep the second window's timeout at 0
|
|
||||||
3. There is a safeguard to stop from launching multiple copies of window2. Only 1 window2 is visible at a time
|
Both windows have buttons to launch popups. The popups are "modal" and thus no other windows will be active
|
||||||
|
|
||||||
|
Copyright 2020 PySimpleGUI.org
|
||||||
"""
|
"""
|
||||||
|
|
||||||
sg.theme('Dark Blue 3')
|
def make_win1():
|
||||||
# Window 1 layout
|
layout = [[sg.Text('This is the FIRST WINDOW'), sg.Text(' ', k='-OUTPUT-')],
|
||||||
layout = [
|
|
||||||
[sg.Text('This is the FIRST WINDOW'), sg.Text(' ', key='-OUTPUT-')],
|
|
||||||
[sg.Text()],
|
[sg.Text()],
|
||||||
[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)
|
||||||
|
|
||||||
|
|
||||||
|
def make_win2():
|
||||||
|
layout = [[sg.Text('The second window')],
|
||||||
|
[sg.Input(key='-IN-')],
|
||||||
|
[sg.Text(size=(25,1), k='-OUTPUT-')],
|
||||||
|
[sg.Button('Show'), 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
|
||||||
|
|
||||||
window = sg.Window('Window Title', layout, location=(800,600))
|
|
||||||
win2_active = False
|
|
||||||
i=0
|
|
||||||
while True: # Event Loop
|
while True: # Event Loop
|
||||||
event, values = window.read(timeout=100)
|
window, event, values = sg.read_all_windows()
|
||||||
if event != sg.TIMEOUT_KEY:
|
if event == sg.WIN_CLOSED or event == 'Exit':
|
||||||
print(i, event, values)
|
window.close()
|
||||||
if event in (sg.WIN_CLOSED, 'Exit'):
|
if window == window2: # if closing win 2, mark as closed
|
||||||
|
window2 = None
|
||||||
|
elif window == window1: # if closing win 1, exit program
|
||||||
break
|
break
|
||||||
elif event == 'Popup':
|
elif event == 'Popup':
|
||||||
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')
|
||||||
i+=1
|
elif event == 'Launch 2nd Window' and not window2:
|
||||||
if event == 'Launch 2nd Window' and not win2_active: # only run if not already showing a window2
|
window2 = make_win2()
|
||||||
win2_active = True
|
elif event == 'Show':
|
||||||
# window 2 layout - note - must be "new" every time a window is created
|
window['-OUTPUT-'].update(f'You enetered {values["-IN-"]}')
|
||||||
layout2 = [
|
|
||||||
[sg.Text('The second window')],
|
|
||||||
[sg.Input(key='-IN-')],
|
|
||||||
[sg.Button('Show'), sg.Button('Exit')]
|
|
||||||
]
|
|
||||||
window2 = sg.Window('Second Window', layout2)
|
|
||||||
# Read window 2's events. Must use timeout of 0
|
|
||||||
if win2_active:
|
|
||||||
# print("reading 2")
|
|
||||||
event, values = window2.read(timeout=100)
|
|
||||||
# print("win2 ", event)
|
|
||||||
if event != sg.TIMEOUT_KEY:
|
|
||||||
print("win2 ", event)
|
|
||||||
if event == 'Exit' or event == sg.WIN_CLOSED:
|
|
||||||
# print("Closing window 2", event)
|
|
||||||
win2_active = False
|
|
||||||
window2.close()
|
|
||||||
if event == 'Show':
|
|
||||||
sg.popup('You entered ', values['-IN-'])
|
|
||||||
|
|
||||||
window.close()
|
window.close()
|
|
@ -0,0 +1,55 @@
|
||||||
|
import PySimpleGUI as sg
|
||||||
|
"""
|
||||||
|
Demo - Running 2 windows with both being active at the same time
|
||||||
|
Three important things to note about this design patter:
|
||||||
|
1. The layout for window 2 is inside of the while loop, just before the call to window2=sg.Window
|
||||||
|
2. The read calls have timeout values of 100 and 0. You can change the 100 to whatever interval you wish
|
||||||
|
but must keep the second window's timeout at 0
|
||||||
|
3. There is a safeguard to stop from launching multiple copies of window2. Only 1 window2 is visible at a time
|
||||||
|
"""
|
||||||
|
|
||||||
|
sg.theme('Dark Blue 3')
|
||||||
|
# Window 1 layout
|
||||||
|
layout = [
|
||||||
|
[sg.Text('This is the FIRST WINDOW'), sg.Text(' ', key='-OUTPUT-')],
|
||||||
|
[sg.Text()],
|
||||||
|
[sg.Button('Launch 2nd Window'), sg.Button('Popup'), sg.Button('Exit')]
|
||||||
|
]
|
||||||
|
|
||||||
|
window = sg.Window('Window Title', layout, location=(800,600))
|
||||||
|
|
||||||
|
win2_active = False
|
||||||
|
i=0
|
||||||
|
while True: # Event Loop
|
||||||
|
event, values = window.read(timeout=100)
|
||||||
|
if event != sg.TIMEOUT_KEY:
|
||||||
|
print(i, event, values)
|
||||||
|
if event in (sg.WIN_CLOSED, 'Exit'):
|
||||||
|
break
|
||||||
|
elif event == 'Popup':
|
||||||
|
sg.popup('This is a BLOCKING popup','all windows remain inactive while popup active')
|
||||||
|
i+=1
|
||||||
|
if event == 'Launch 2nd Window' and not win2_active: # only run if not already showing a window2
|
||||||
|
win2_active = True
|
||||||
|
# window 2 layout - note - must be "new" every time a window is created
|
||||||
|
layout2 = [
|
||||||
|
[sg.Text('The second window')],
|
||||||
|
[sg.Input(key='-IN-')],
|
||||||
|
[sg.Button('Show'), sg.Button('Exit')]
|
||||||
|
]
|
||||||
|
window2 = sg.Window('Second Window', layout2)
|
||||||
|
# Read window 2's events. Must use timeout of 0
|
||||||
|
if win2_active:
|
||||||
|
# print("reading 2")
|
||||||
|
event, values = window2.read(timeout=100)
|
||||||
|
# print("win2 ", event)
|
||||||
|
if event != sg.TIMEOUT_KEY:
|
||||||
|
print("win2 ", event)
|
||||||
|
if event == 'Exit' or event == sg.WIN_CLOSED:
|
||||||
|
# print("Closing window 2", event)
|
||||||
|
win2_active = False
|
||||||
|
window2.close()
|
||||||
|
if event == 'Show':
|
||||||
|
sg.popup('You entered ', values['-IN-'])
|
||||||
|
|
||||||
|
window.close()
|
Loading…
Reference in New Issue