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
|
|
|
|
|
2019-09-20 21:31:00 +00:00
|
|
|
1. A "One Shot" window
|
2020-05-07 10:22:59 +00:00
|
|
|
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
|
2018-09-27 20:24:09 +00:00
|
|
|
"""
|
2020-05-07 10:22:59 +00:00
|
|
|
# -----------------------------------#
|
|
|
|
# DESIGN PATTERN 1 - One-shot Window #
|
|
|
|
# -----------------------------------#
|
2019-12-06 02:21:08 +00:00
|
|
|
import PySimpleGUI as sg
|
|
|
|
|
2019-09-20 21:31:00 +00:00
|
|
|
layout = [[ sg.Text('My Oneshot') ],
|
2020-05-07 10:22:59 +00:00
|
|
|
[ sg.Input(key='-IN-') ],
|
2019-09-20 21:31:00 +00:00
|
|
|
[ sg.Button('OK') ]]
|
2018-09-24 22:28:54 +00:00
|
|
|
|
2020-05-07 10:22:59 +00:00
|
|
|
window = sg.Window('Design Pattern 1', layout)
|
2019-09-20 21:31:00 +00:00
|
|
|
event, values = window.read()
|
|
|
|
window.close()
|
2018-09-24 22:28:54 +00:00
|
|
|
|
2018-09-27 20:24:09 +00:00
|
|
|
|
2020-05-07 10:22:59 +00:00
|
|
|
# ---------------------------------------------#
|
|
|
|
# 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)
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-09-27 20:24:09 +00:00
|
|
|
# -------------------------------------#
|
2020-05-07 10:22:59 +00:00
|
|
|
# DESIGN PATTERN 3 - Persistent Window #
|
2018-09-27 20:24:09 +00:00
|
|
|
# -------------------------------------#
|
2019-12-06 02:21:08 +00:00
|
|
|
import PySimpleGUI as sg
|
2018-09-24 22:28:54 +00:00
|
|
|
|
2020-05-07 10:22:59 +00:00
|
|
|
layout = [[sg.Text('My layout')],
|
|
|
|
[sg.Input(key='-INPUT-')],
|
|
|
|
[sg.Button('OK'), sg.Button('Cancel')] ]
|
2018-09-24 22:28:54 +00:00
|
|
|
|
2020-05-07 10:22:59 +00:00
|
|
|
window = sg.Window('Design Pattern 3 - Persistent Window', layout)
|
2018-09-24 22:28:54 +00:00
|
|
|
|
|
|
|
while True: # Event Loop
|
2019-09-20 21:31:00 +00:00
|
|
|
event, values = window.read()
|
2020-05-07 10:22:59 +00:00
|
|
|
if event == sg.WIN_CLOSED or event == 'Cancel':
|
2018-09-24 22:28:54 +00:00
|
|
|
break
|
2019-09-20 21:31:00 +00:00
|
|
|
window.close()
|
2018-09-24 22:28:54 +00:00
|
|
|
|
2018-09-27 20:24:09 +00:00
|
|
|
# ------------------------------------------------------------------#
|
2020-05-07 10:22:59 +00:00
|
|
|
# DESIGN PATTERN 4 - Persistent Window with "early update" required #
|
2018-09-27 20:24:09 +00:00
|
|
|
# ------------------------------------------------------------------#
|
2019-12-06 02:21:08 +00:00
|
|
|
import PySimpleGUI as sg
|
|
|
|
|
2020-05-07 10:22:59 +00:00
|
|
|
layout = [[ sg.Text('My layout') ],
|
|
|
|
[sg.Input(key='-INPUT-')],
|
|
|
|
[sg.Text('Some text will be output here', key='-TEXT-KEY-')],
|
2019-09-20 21:31:00 +00:00
|
|
|
[ sg.Button('OK'), sg.Button('Cancel') ]]
|
2018-09-24 22:28:54 +00:00
|
|
|
|
2020-05-07 10:22:59 +00:00
|
|
|
window = sg.Window('Design Pattern 4', layout, finalize=True)
|
2018-09-24 22:28:54 +00:00
|
|
|
|
2020-05-07 10:22:59 +00:00
|
|
|
# Change the text field. Finalize allows us to do this
|
|
|
|
window['-TEXT-KEY-'].update('Modified before event loop')
|
2018-10-29 00:01:03 +00:00
|
|
|
|
2018-09-24 22:28:54 +00:00
|
|
|
while True: # Event Loop
|
2019-09-20 21:31:00 +00:00
|
|
|
event, values = window.read()
|
2020-05-07 10:22:59 +00:00
|
|
|
if event == sg.WIN_CLOSED or event == 'Cancel':
|
2019-09-20 21:31:00 +00:00
|
|
|
break
|
2020-05-07 10:22:59 +00:00
|
|
|
if event == 'OK':
|
|
|
|
window['-TEXT-KEY-'].update(values['-INPUT-'])
|
2019-09-20 21:31:00 +00:00
|
|
|
window.close()
|