PySimpleGUI/DemoPrograms/Demo_Design_Patterns.py

62 lines
1.8 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 "One Shot" window
2018-09-27 20:24:09 +00:00
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
2018-09-27 20:24:09 +00:00
"""
# ---------------------------------#
# DESIGN PATTERN 1 - Simple Window #
# ---------------------------------#
2019-12-06 02:21:08 +00:00
import PySimpleGUI as sg
sg.change_look_and_feel('Dark Blue 3')
2018-09-24 22:28:54 +00:00
layout = [[ sg.Text('My Oneshot') ],
[ sg.Button('OK') ]]
2018-09-24 22:28:54 +00:00
window = sg.Window('My Oneshot', layout)
event, values = window.read()
window.close()
2018-09-24 22:28:54 +00:00
2018-09-27 20:24:09 +00:00
# -------------------------------------#
# DESIGN PATTERN 2 - Persistent Window #
# -------------------------------------#
2019-12-06 02:21:08 +00:00
import PySimpleGUI as sg
2018-09-24 22:28:54 +00:00
2019-12-06 02:21:08 +00:00
sg.change_look_and_feel('Dark Blue 3')
layout = [[ sg.Text('My layout', text_color='red') ],
[ sg.Input(key='-INPUT-')],
[ sg.Button('OK'), sg.Button('Cancel') ]]
2018-09-24 22:28:54 +00:00
2019-12-06 02:21:08 +00:00
window = sg.Window('Design Pattern 2 - Persistent Window', layout)
2018-09-24 22:28:54 +00:00
while True: # Event Loop
event, values = window.read()
if event in (None, 'Cancel'):
2018-09-24 22:28:54 +00:00
break
window.close()
2018-09-24 22:28:54 +00:00
2018-09-27 20:24:09 +00:00
# ------------------------------------------------------------------#
# DESIGN PATTERN 3 - Persistent Window with "early update" required #
# ------------------------------------------------------------------#
2019-12-06 02:21:08 +00:00
import PySimpleGUI as sg
sg.change_look_and_feel('Dark Blue 3')
2018-09-24 22:28:54 +00:00
layout = [[ sg.Text('My layout', key='-TEXT-KEY-') ],
[ sg.Button('OK'), sg.Button('Cancel') ]]
2018-09-24 22:28:54 +00:00
window = sg.Window('Design Pattern 3', layout, finalize=True)
2018-09-24 22:28:54 +00:00
window['-TEXT-KEY-'].update('NEW Text') # Change the text field. Finalize allows us to do this
2018-10-29 00:01:03 +00:00
2018-09-24 22:28:54 +00:00
while True: # Event Loop
event, values = window.read()
if event in (None, 'Cancel'):
break
window.close()