PySimpleGUI/DemoPrograms/Demo_Design_Patterns.py

60 lines
1.7 KiB
Python
Raw Normal View History

2018-09-27 20:24:09 +00:00
"""
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
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
"""
# ---------------------------------#
# DESIGN PATTERN 1 - Simple Window #
# ---------------------------------#
import sys
if sys.version_info[0] >= 3:
import PySimpleGUI as sg
else:
import PySimpleGUI27 as sg
2018-09-24 22:28:54 +00:00
layout = [[ sg.Text('My layout') ]]
window = sg.Window('My window').Layout(layout)
event, values = window.Read()
2018-09-24 22:28:54 +00:00
2018-09-27 20:24:09 +00:00
# -------------------------------------#
# DESIGN PATTERN 2 - Persistent Window #
# -------------------------------------#
import sys
if sys.version_info[0] >= 3:
import PySimpleGUI as sg
else:
import PySimpleGUI27 as sg
2018-09-24 22:28:54 +00:00
layout = [[ sg.Text('My layout') ]]
window = sg.Window('My new window').Layout(layout)
while True: # Event Loop
event, values = window.Read()
if event is None:
2018-09-24 22:28:54 +00:00
break
2018-09-27 20:24:09 +00:00
# ------------------------------------------------------------------#
# 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
2018-09-24 22:28:54 +00:00
layout = [[ sg.Text('My layout') ]]
window = sg.Window('My new window').Layout(layout).Finalize()
2018-10-29 00:01:03 +00:00
# if you have operations on elements that must take place before the event loop, do them here
2018-09-24 22:28:54 +00:00
while True: # Event Loop
event, values = window.Read()
if event is None:
2018-09-24 22:28:54 +00:00
break