Refreshing demos. Updating to use newer techniques

This commit is contained in:
PySimpleGUI 2019-09-20 17:31:00 -04:00
parent e5e1021516
commit f7deaadf97
8 changed files with 33 additions and 696 deletions

View file

@ -2,59 +2,53 @@
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 window that closes when a "submit" type button is clicked
1. A "One Shot" window
2. A persistent window that stays open after button clicks (uses an event loop)
3. A persistent window that needs access to the elements' interface variables
3. A persistent window that need to perform Update of an element before the window.read
"""
# ---------------------------------#
# DESIGN PATTERN 1 - Simple Window #
# ---------------------------------#
import sys
if sys.version_info[0] >= 3:
import PySimpleGUI as sg
else:
import PySimpleGUI27 as sg
import PySimpleGUI as sg
layout = [[ sg.Text('My layout') ]]
layout = [[ sg.Text('My Oneshot') ],
[ sg.Button('OK') ]]
window = sg.Window('My window').Layout(layout)
event, values = window.Read()
window = sg.Window('My Oneshot', layout)
event, values = window.read()
window.close()
# -------------------------------------#
# DESIGN PATTERN 2 - Persistent Window #
# -------------------------------------#
import sys
if sys.version_info[0] >= 3:
import PySimpleGUI as sg
else:
import PySimpleGUI27 as sg
import PySimpleGUI as sg
layout = [[ sg.Text('My layout') ]]
layout = [[ sg.Text('My layout') ],
[ sg.Button('OK'), sg.Button('Cancel') ]]
window = sg.Window('My new window').Layout(layout)
window = sg.Window('Design Pattern 2', layout)
while True: # Event Loop
event, values = window.Read()
if event is None:
event, values = window.read()
if event in (None, 'Cancel'):
break
window.close()
# ------------------------------------------------------------------#
# DESIGN PATTERN 3 - Persistent Window with "early update" required #
# ------------------------------------------------------------------#
import sys
if sys.version_info[0] >= 3:
import PySimpleGUI as sg
else:
import PySimpleGUI27 as sg
import PySimpleGUI as sg
layout = [[ sg.Text('My layout') ]]
layout = [[ sg.Text('My layout', key='-TEXT-KEY-') ],
[ sg.Button('OK'), sg.Button('Cancel') ]]
window = sg.Window('My new window').Layout(layout).Finalize()
window = sg.Window('Design Pattern 3', layout, finalize=True)
# if you have operations on elements that must take place before the event loop, do them here
window['-TEXT-KEY-'].Update('NEW Text') # Change the text field. Finalize allows us to do this
while True: # Event Loop
event, values = window.Read()
if event is None:
break
event, values = window.read()
if event in (None, 'Cancel'):
break
window.close()