WIN_CLOSED bulk update. No more test for event is None, use WIN_CLOSED instead

This commit is contained in:
PySimpleGUI 2020-05-07 06:22:59 -04:00
parent 6174b355a6
commit 0076b461f5
135 changed files with 395 additions and 349 deletions

View file

@ -3,59 +3,69 @@ When creating a new PySimpleGUI program from scratch, start here.
These are the accepted design patterns that cover the two primary use cases
1. A "One Shot" window
2. A persistent window that stays open after button clicks (uses an event loop)
3. A persistent window that need to perform update of an element before the window.read
2. A "One Shot" window in 1 line of code
3. A persistent window that stays open after button clicks (uses an event loop)
4. A persistent window that need to perform update of an element before the window.read
"""
# ---------------------------------#
# DESIGN PATTERN 1 - Simple Window #
# ---------------------------------#
# -----------------------------------#
# DESIGN PATTERN 1 - One-shot Window #
# -----------------------------------#
import PySimpleGUI as sg
sg.theme('Dark Blue 3')
layout = [[ sg.Text('My Oneshot') ],
[ sg.Input(key='-IN-') ],
[ sg.Button('OK') ]]
window = sg.Window('My Oneshot', layout)
window = sg.Window('Design Pattern 1', layout)
event, values = window.read()
window.close()
# ---------------------------------------------#
# DESIGN PATTERN 2 - One-shot Window in 1 line #
# ---------------------------------------------#
import PySimpleGUI as sg
event, values = sg.Window('Design Pattern 2', [[sg.Text('My Oneshot')],[sg.Input(key='-IN-')], [ sg.Button('OK') ]]).read(close=True)
# -------------------------------------#
# DESIGN PATTERN 2 - Persistent Window #
# DESIGN PATTERN 3 - Persistent Window #
# -------------------------------------#
import PySimpleGUI as sg
sg.theme('Dark Blue 3')
layout = [[sg.Text('My layout')],
[sg.Input(key='-INPUT-')],
[sg.Button('OK'), sg.Button('Cancel')] ]
layout = [[ sg.Text('My layout', text_color='red') ],
[ sg.Input(key='-INPUT-')],
[ sg.Button('OK'), sg.Button('Cancel') ]]
window = sg.Window('Design Pattern 2 - Persistent Window', layout)
window = sg.Window('Design Pattern 3 - Persistent Window', layout)
while True: # Event Loop
event, values = window.read()
if event in (None, 'Cancel'):
if event == sg.WIN_CLOSED or event == 'Cancel':
break
window.close()
# ------------------------------------------------------------------#
# DESIGN PATTERN 3 - Persistent Window with "early update" required #
# DESIGN PATTERN 4 - Persistent Window with "early update" required #
# ------------------------------------------------------------------#
import PySimpleGUI as sg
sg.theme('Dark Blue 3')
layout = [[ sg.Text('My layout', key='-TEXT-KEY-') ],
layout = [[ sg.Text('My layout') ],
[sg.Input(key='-INPUT-')],
[sg.Text('Some text will be output here', key='-TEXT-KEY-')],
[ sg.Button('OK'), sg.Button('Cancel') ]]
window = sg.Window('Design Pattern 3', layout, finalize=True)
window = sg.Window('Design Pattern 4', layout, finalize=True)
window['-TEXT-KEY-'].update('NEW Text') # Change the text field. Finalize allows us to do this
# Change the text field. Finalize allows us to do this
window['-TEXT-KEY-'].update('Modified before event loop')
while True: # Event Loop
event, values = window.read()
if event in (None, 'Cancel'):
if event == sg.WIN_CLOSED or event == 'Cancel':
break
if event == 'OK':
window['-TEXT-KEY-'].update(values['-INPUT-'])
window.close()