forked from PySimpleGUI/PySimpleGUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDemo_Design_Patterns.py
More file actions
54 lines (41 loc) · 1.64 KB
/
Demo_Design_Patterns.py
File metadata and controls
54 lines (41 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""
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
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
"""
# ---------------------------------#
# DESIGN PATTERN 1 - Simple Window #
# ---------------------------------#
import PySimpleGUI as sg
layout = [[ sg.Text('My Oneshot') ],
[ sg.Button('OK') ]]
window = sg.Window('My Oneshot', layout)
event, values = window.read()
window.close()
# -------------------------------------#
# DESIGN PATTERN 2 - Persistent Window #
# -------------------------------------#
import PySimpleGUI as sg
layout = [[ sg.Text('My layout') ],
[ sg.Button('OK'), sg.Button('Cancel') ]]
window = sg.Window('Design Pattern 2', layout)
while True: # Event Loop
event, values = window.read()
if event in (None, 'Cancel'):
break
window.close()
# ------------------------------------------------------------------#
# DESIGN PATTERN 3 - Persistent Window with "early update" required #
# ------------------------------------------------------------------#
import PySimpleGUI as sg
layout = [[ sg.Text('My layout', key='-TEXT-KEY-') ],
[ sg.Button('OK'), sg.Button('Cancel') ]]
window = sg.Window('Design Pattern 3', layout, finalize=True)
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 in (None, 'Cancel'):
break
window.close()