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 #
|
|
|
|
# ---------------------------------#
|
2018-09-28 18:57:37 +00:00
|
|
|
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)
|
|
|
|
button, value = window.Read()
|
|
|
|
|
2018-09-27 20:24:09 +00:00
|
|
|
|
|
|
|
# -------------------------------------#
|
|
|
|
# DESIGN PATTERN 2 - Persistent Window #
|
|
|
|
# -------------------------------------#
|
2018-09-28 18:57:37 +00:00
|
|
|
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
|
|
|
|
button, value = window.Read()
|
|
|
|
if button is None:
|
|
|
|
break
|
|
|
|
|
2018-09-27 20:24:09 +00:00
|
|
|
# ------------------------------------------------------------------#
|
|
|
|
# DESIGN PATTERN 3 - Persistent Window with "early update" required #
|
|
|
|
# ------------------------------------------------------------------#
|
2018-09-28 18:57:37 +00:00
|
|
|
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()
|
|
|
|
|
|
|
|
while True: # Event Loop
|
|
|
|
button, value = window.Read()
|
|
|
|
if button is None:
|
|
|
|
break
|