FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
PySimpleGUI/DemoPrograms/Demo_Design_Patterns.py at master · DevLang-Python/PySimpleGUI · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
DevLang-Python
/
PySimpleGUI
Public
forked from
PySimpleGUI/PySimpleGUI
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
PySimpleGUI
/
DemoPrograms
/
Demo_Design_Patterns.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
71 lines (54 loc) · 2.3 KB
Breadcrumbs
PySimpleGUI
/
DemoPrograms
/
Demo_Design_Patterns.py
Copy path
File metadata and controls
71 lines (54 loc) · 2.3 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""
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 "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
"""
# -----------------------------------#
# DESIGN PATTERN 1 - One-shot Window #
# -----------------------------------#
import
PySimpleGUI
as
sg
layout
=
[[
sg
.
Text
(
'My Oneshot'
) ],
[
sg
.
Input
(
key
=
'-IN-'
) ],
[
sg
.
Button
(
'OK'
) ]]
window
=
sg
.
Window
(
'Design Pattern 1'
,
layout
)
event
,
values
=
window
.
read
()
window
.
close
()
# ---------------------------------------------#
# 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
)
# -------------------------------------#
# DESIGN PATTERN 3 - Persistent Window #
# -------------------------------------#
import
PySimpleGUI
as
sg
layout
=
[[
sg
.
Text
(
'My layout'
)],
[
sg
.
Input
(
key
=
'-INPUT-'
)],
[
sg
.
Button
(
'OK'
),
sg
.
Button
(
'Cancel'
)] ]
window
=
sg
.
Window
(
'Design Pattern 3 - Persistent Window'
,
layout
)
while
True
:
# Event Loop
event
,
values
=
window
.
read
()
if
event
==
sg
.
WIN_CLOSED
or
event
==
'Cancel'
:
break
window
.
close
()
# ------------------------------------------------------------------#
# DESIGN PATTERN 4 - Persistent Window with "early update" required #
# ------------------------------------------------------------------#
import
PySimpleGUI
as
sg
layout
=
[[
sg
.
Text
(
'My layout'
) ],
[
sg
.
Input
(
key
=
'-INPUT-'
)],
[
sg
.
Text
(
'Some text will be output here'
,
key
=
'-TEXT-KEY-'
)],
[
sg
.
Button
(
'OK'
),
sg
.
Button
(
'Cancel'
) ]]
window
=
sg
.
Window
(
'Design Pattern 4'
,
layout
,
finalize
=
True
)
# Change the text field. Finalize allows us to do this
window
[
'-TEXT-KEY-'
].
update
(
'Modified before event loop'
)
while
True
:
# Event Loop
event
,
values
=
window
.
read
()
if
event
==
sg
.
WIN_CLOSED
or
event
==
'Cancel'
:
break
if
event
==
'OK'
:
window
[
'-TEXT-KEY-'
].
update
(
values
[
'-INPUT-'
])
window
.
close
()
Back
|
FazBrowse Home
|
New Git URL