SwiftGUI 0.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- swiftgui-0.0.1/LICENSE +201 -0
- swiftgui-0.0.1/PKG-INFO +104 -0
- swiftgui-0.0.1/README.md +89 -0
- swiftgui-0.0.1/pyproject.toml +26 -0
- swiftgui-0.0.1/src/SwiftGUI/AnyElement.py +7 -0
- swiftgui-0.0.1/src/SwiftGUI/Base.py +307 -0
- swiftgui-0.0.1/src/SwiftGUI/ElementFlags.py +9 -0
- swiftgui-0.0.1/src/SwiftGUI/Events.py +38 -0
- swiftgui-0.0.1/src/SwiftGUI/KeyFunctions.py +25 -0
- swiftgui-0.0.1/src/SwiftGUI/KeyManager.py +65 -0
- swiftgui-0.0.1/src/SwiftGUI/Widgets.py +205 -0
- swiftgui-0.0.1/src/SwiftGUI/WidgetsAdvanced.py +58 -0
- swiftgui-0.0.1/src/SwiftGUI/Windows.py +162 -0
- swiftgui-0.0.1/src/SwiftGUI/__init__.py +11 -0
swiftgui-0.0.1/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
swiftgui-0.0.1/PKG-INFO
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: SwiftGUI
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: An easy-to-use package for creating and using simple GUIs
|
|
5
|
+
License: Apache-2.0
|
|
6
|
+
Author: Eric aka LeButch
|
|
7
|
+
Author-email: cheesecaketv53+pypi@gmail.com
|
|
8
|
+
Requires-Python: >=3.9
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Project-URL: Homepage, https://github.com/CheesecakeTV/SwiftGUI
|
|
12
|
+
Project-URL: issues, https://github.com/CheesecakeTV/SwiftGUI/issues
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
# Not even close to being finished
|
|
17
|
+
But I'd love to hear your suggestions.
|
|
18
|
+
|
|
19
|
+
# swiftGUI
|
|
20
|
+
|
|
21
|
+
A python-package to quickly create common user interfaces.
|
|
22
|
+
|
|
23
|
+
I really liked PySimpleGUI (before they went "premium"),
|
|
24
|
+
but their codebase is an utter mess.
|
|
25
|
+
|
|
26
|
+
swiftGUI is more leightweight and aims at being faster to use and easier to expand.
|
|
27
|
+
|
|
28
|
+
## Why sqiftGUI?
|
|
29
|
+
I have a lot of experience with PySimpleGUI, used it for years.
|
|
30
|
+
It is very useful, but has a lot of super annoying aspects.
|
|
31
|
+
|
|
32
|
+
This is what I hate about it:
|
|
33
|
+
- PySimpleGUI is pretty much impossible to expand, because of the codebase.
|
|
34
|
+
- PySimpleGUI is only efficient for smaller layouts.
|
|
35
|
+
There are just some things, you do in most bigger layouts,
|
|
36
|
+
which are very annoying and time-consuming.
|
|
37
|
+
- PySimpleGUI doesn't really let you copy parts of your layout automatically.
|
|
38
|
+
If you want to copy it, you must copy code (or create a custom function).
|
|
39
|
+
When you want to change certain aspects of all copied elements,
|
|
40
|
+
you need to change it in every copy. Very inefficient.
|
|
41
|
+
- The only ways to convey events in PySimpleGUI is through breaking out of window.read(), or
|
|
42
|
+
writing events onto the tkinter-widget directly.
|
|
43
|
+
Due to that, in bigger layouts, the code becomes cluttered quickly and has a lot of redundancy.
|
|
44
|
+
|
|
45
|
+
swiftGUI is the answer to these negative aspects.
|
|
46
|
+
It allows you to create bigger GUIs, quickly and elegantly.
|
|
47
|
+
|
|
48
|
+
## Features and Differences to PySimpleGUI
|
|
49
|
+
Disclaimer: I did not copy any code of PySimpleGUI.
|
|
50
|
+
This library is completely independent from PySimpleGUI and aims at making it obsolete.
|
|
51
|
+
|
|
52
|
+
Some of these features haven't been implemented yet, but the package is created in a way
|
|
53
|
+
that all of these will be possible without any major hussle.
|
|
54
|
+
|
|
55
|
+
### Layout
|
|
56
|
+
The way you create layouts is pretty much the same as in PySimpleGUI.
|
|
57
|
+
|
|
58
|
+
However, in swiftGUI it is possible to copy parts of the layout.
|
|
59
|
+
|
|
60
|
+
### Events
|
|
61
|
+
In PySimpleGUI, every event has to have a key and always breaks out of `window.read()`,
|
|
62
|
+
slowing down the code.
|
|
63
|
+
|
|
64
|
+
In SwiftGUI, you have the option to pass "key-functions" (additionally to the normal key).
|
|
65
|
+
When an event occurs, these functions will be evoked too.
|
|
66
|
+
|
|
67
|
+
E.g.: Let's say you want to add a button that clears out an input-element.
|
|
68
|
+
In PySimpleGUI, you would need to give that button its own key, add an if-statement
|
|
69
|
+
to the main loop, just to make the call `window["InputKey"]("")"`.
|
|
70
|
+
|
|
71
|
+
In swiftGUI, the only thing you need to do is pass a lambda-function as a key-function to
|
|
72
|
+
the button: `sg.Button(...,key_function=lambda w,*_:w["InputKey"].set_value("""))`.\
|
|
73
|
+
Done.\
|
|
74
|
+
No breaking out of window.loop(), no if-statement, no possible key used,
|
|
75
|
+
not even an additional line of code.
|
|
76
|
+
|
|
77
|
+
Additionally, there are a couple of pre-made "key-functions" you can configure and use.
|
|
78
|
+
The clearing out of an input is one of them, so no need to write the lambda.
|
|
79
|
+
|
|
80
|
+
### Elements/Widgets
|
|
81
|
+
Additional to the standard-widgets of Tkinter that PySimpleGUI has,
|
|
82
|
+
SwiftGUI offers a selection of "combined elements" and elements with extended functionality.
|
|
83
|
+
|
|
84
|
+
These combined elements contain multiple tk-widgets that mostly get interpreted as a single value.
|
|
85
|
+
|
|
86
|
+
E.g. the `Form`-Element consists of multiple rows of text-input-combinations.
|
|
87
|
+
The values can either be packed into a dictionary to use less keys,
|
|
88
|
+
or every `Input` gets its own key.
|
|
89
|
+
|
|
90
|
+
This is something most layouts need to have, but with PySimpleGUI, you need to create
|
|
91
|
+
every Element one by one, or create a wrapper (which is very janky due to PySimpleGUIs codebase).
|
|
92
|
+
|
|
93
|
+
### Expandability
|
|
94
|
+
|
|
95
|
+
SwiftGUI aims at being as easy to expand as possible.
|
|
96
|
+
|
|
97
|
+
For beginners, I recommend copying an element from `WidgetsAdvanced.py` that
|
|
98
|
+
fits what you are looking to implement best.
|
|
99
|
+
|
|
100
|
+
If you know, how to properly use inheritance, you should start with `Base.py`.
|
|
101
|
+
|
|
102
|
+
The descriptions (docstrings) of all inherited methods can be found in `Base.py`
|
|
103
|
+
|
|
104
|
+
|
swiftgui-0.0.1/README.md
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
|
|
2
|
+
# Not even close to being finished
|
|
3
|
+
But I'd love to hear your suggestions.
|
|
4
|
+
|
|
5
|
+
# swiftGUI
|
|
6
|
+
|
|
7
|
+
A python-package to quickly create common user interfaces.
|
|
8
|
+
|
|
9
|
+
I really liked PySimpleGUI (before they went "premium"),
|
|
10
|
+
but their codebase is an utter mess.
|
|
11
|
+
|
|
12
|
+
swiftGUI is more leightweight and aims at being faster to use and easier to expand.
|
|
13
|
+
|
|
14
|
+
## Why sqiftGUI?
|
|
15
|
+
I have a lot of experience with PySimpleGUI, used it for years.
|
|
16
|
+
It is very useful, but has a lot of super annoying aspects.
|
|
17
|
+
|
|
18
|
+
This is what I hate about it:
|
|
19
|
+
- PySimpleGUI is pretty much impossible to expand, because of the codebase.
|
|
20
|
+
- PySimpleGUI is only efficient for smaller layouts.
|
|
21
|
+
There are just some things, you do in most bigger layouts,
|
|
22
|
+
which are very annoying and time-consuming.
|
|
23
|
+
- PySimpleGUI doesn't really let you copy parts of your layout automatically.
|
|
24
|
+
If you want to copy it, you must copy code (or create a custom function).
|
|
25
|
+
When you want to change certain aspects of all copied elements,
|
|
26
|
+
you need to change it in every copy. Very inefficient.
|
|
27
|
+
- The only ways to convey events in PySimpleGUI is through breaking out of window.read(), or
|
|
28
|
+
writing events onto the tkinter-widget directly.
|
|
29
|
+
Due to that, in bigger layouts, the code becomes cluttered quickly and has a lot of redundancy.
|
|
30
|
+
|
|
31
|
+
swiftGUI is the answer to these negative aspects.
|
|
32
|
+
It allows you to create bigger GUIs, quickly and elegantly.
|
|
33
|
+
|
|
34
|
+
## Features and Differences to PySimpleGUI
|
|
35
|
+
Disclaimer: I did not copy any code of PySimpleGUI.
|
|
36
|
+
This library is completely independent from PySimpleGUI and aims at making it obsolete.
|
|
37
|
+
|
|
38
|
+
Some of these features haven't been implemented yet, but the package is created in a way
|
|
39
|
+
that all of these will be possible without any major hussle.
|
|
40
|
+
|
|
41
|
+
### Layout
|
|
42
|
+
The way you create layouts is pretty much the same as in PySimpleGUI.
|
|
43
|
+
|
|
44
|
+
However, in swiftGUI it is possible to copy parts of the layout.
|
|
45
|
+
|
|
46
|
+
### Events
|
|
47
|
+
In PySimpleGUI, every event has to have a key and always breaks out of `window.read()`,
|
|
48
|
+
slowing down the code.
|
|
49
|
+
|
|
50
|
+
In SwiftGUI, you have the option to pass "key-functions" (additionally to the normal key).
|
|
51
|
+
When an event occurs, these functions will be evoked too.
|
|
52
|
+
|
|
53
|
+
E.g.: Let's say you want to add a button that clears out an input-element.
|
|
54
|
+
In PySimpleGUI, you would need to give that button its own key, add an if-statement
|
|
55
|
+
to the main loop, just to make the call `window["InputKey"]("")"`.
|
|
56
|
+
|
|
57
|
+
In swiftGUI, the only thing you need to do is pass a lambda-function as a key-function to
|
|
58
|
+
the button: `sg.Button(...,key_function=lambda w,*_:w["InputKey"].set_value("""))`.\
|
|
59
|
+
Done.\
|
|
60
|
+
No breaking out of window.loop(), no if-statement, no possible key used,
|
|
61
|
+
not even an additional line of code.
|
|
62
|
+
|
|
63
|
+
Additionally, there are a couple of pre-made "key-functions" you can configure and use.
|
|
64
|
+
The clearing out of an input is one of them, so no need to write the lambda.
|
|
65
|
+
|
|
66
|
+
### Elements/Widgets
|
|
67
|
+
Additional to the standard-widgets of Tkinter that PySimpleGUI has,
|
|
68
|
+
SwiftGUI offers a selection of "combined elements" and elements with extended functionality.
|
|
69
|
+
|
|
70
|
+
These combined elements contain multiple tk-widgets that mostly get interpreted as a single value.
|
|
71
|
+
|
|
72
|
+
E.g. the `Form`-Element consists of multiple rows of text-input-combinations.
|
|
73
|
+
The values can either be packed into a dictionary to use less keys,
|
|
74
|
+
or every `Input` gets its own key.
|
|
75
|
+
|
|
76
|
+
This is something most layouts need to have, but with PySimpleGUI, you need to create
|
|
77
|
+
every Element one by one, or create a wrapper (which is very janky due to PySimpleGUIs codebase).
|
|
78
|
+
|
|
79
|
+
### Expandability
|
|
80
|
+
|
|
81
|
+
SwiftGUI aims at being as easy to expand as possible.
|
|
82
|
+
|
|
83
|
+
For beginners, I recommend copying an element from `WidgetsAdvanced.py` that
|
|
84
|
+
fits what you are looking to implement best.
|
|
85
|
+
|
|
86
|
+
If you know, how to properly use inheritance, you should start with `Base.py`.
|
|
87
|
+
|
|
88
|
+
The descriptions (docstrings) of all inherited methods can be found in `Base.py`
|
|
89
|
+
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "SwiftGUI"
|
|
3
|
+
version = "0.0.1"
|
|
4
|
+
packages = [
|
|
5
|
+
{ include = "SwiftGUI", from = "src" }
|
|
6
|
+
]
|
|
7
|
+
authors = [
|
|
8
|
+
{ name="Eric aka LeButch", email="cheesecaketv53+pypi@gmail.com" },
|
|
9
|
+
]
|
|
10
|
+
description = "An easy-to-use package for creating and using simple GUIs"
|
|
11
|
+
readme = "README.md"
|
|
12
|
+
requires-python = ">=3.9"
|
|
13
|
+
classifiers = [
|
|
14
|
+
"Programming Language :: Python :: 3",
|
|
15
|
+
"Operating System :: OS Independent",
|
|
16
|
+
]
|
|
17
|
+
license = "Apache-2.0"
|
|
18
|
+
license-files = ["LICEN[CS]E*"]
|
|
19
|
+
#
|
|
20
|
+
[project.urls]
|
|
21
|
+
Homepage = "https://github.com/CheesecakeTV/SwiftGUI"
|
|
22
|
+
issues = "https://github.com/CheesecakeTV/SwiftGUI/issues"
|
|
23
|
+
|
|
24
|
+
[build-system]
|
|
25
|
+
requires = ["poetry-core>=2.0.0,<3.0.0"]
|
|
26
|
+
build-backend = "poetry.core.masonry.api"
|
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
from collections.abc import Iterable, Callable
|
|
2
|
+
from typing import Literal, Self, Union
|
|
3
|
+
import tkinter as tk
|
|
4
|
+
|
|
5
|
+
from SwiftGUI import Event
|
|
6
|
+
from SwiftGUI.ElementFlags import ElementFlag
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class BaseElement:
|
|
10
|
+
"""
|
|
11
|
+
Base for every element and widget.
|
|
12
|
+
|
|
13
|
+
The different to BaseWidget is that BaseWidget is designed for a single tk-Widget.
|
|
14
|
+
BaseElement allows you to do whatever the f you want, it's just a class pattern.
|
|
15
|
+
"""
|
|
16
|
+
parent:Self = None # next higher Element
|
|
17
|
+
_fake_tk_element:tk.Widget = None # This gets returned when parent is None
|
|
18
|
+
_element_flags:set[ElementFlag] # Properties the element has
|
|
19
|
+
window = None # Main Window
|
|
20
|
+
|
|
21
|
+
key:any = None # If given, this will be written to the event-value. Elements without a key can not throw key-events
|
|
22
|
+
_key_function: Callable | Iterable[Callable] = None # Called as an event
|
|
23
|
+
|
|
24
|
+
_key_function_send_wev:bool = False # True, if window, event, value should be sent to key_function
|
|
25
|
+
_key_function_send_val:bool = False # True, if current event value shall be sent to key_function
|
|
26
|
+
# If both are True, it will be sent like this: window, event, values, value
|
|
27
|
+
|
|
28
|
+
def _init(self,parent:"BaseElement",window):
|
|
29
|
+
"""
|
|
30
|
+
Not __init__
|
|
31
|
+
|
|
32
|
+
This gets called when the window is initialized.
|
|
33
|
+
|
|
34
|
+
:param parent: Element to contain this element
|
|
35
|
+
:param window: Main Window
|
|
36
|
+
:return:
|
|
37
|
+
"""
|
|
38
|
+
self._flag_init()
|
|
39
|
+
self._normal_init(parent,window)
|
|
40
|
+
self._personal_init()
|
|
41
|
+
|
|
42
|
+
def _flag_init(self):
|
|
43
|
+
"""
|
|
44
|
+
Override this to add flags to the element.
|
|
45
|
+
DONT FORGET TO CALL THE SUPER METHOD!
|
|
46
|
+
:return:
|
|
47
|
+
"""
|
|
48
|
+
# DON'T FORGET THIS CALL: super()._flag_init()
|
|
49
|
+
# self.add_flags(...)
|
|
50
|
+
pass
|
|
51
|
+
|
|
52
|
+
@property
|
|
53
|
+
def element_flags(self) -> set[ElementFlag]:
|
|
54
|
+
if not hasattr(self,"_element_flags"):
|
|
55
|
+
self._element_flags = set()
|
|
56
|
+
|
|
57
|
+
return self._element_flags
|
|
58
|
+
|
|
59
|
+
def add_flags(self,*flags:ElementFlag):
|
|
60
|
+
"""
|
|
61
|
+
Add a flag to this element.
|
|
62
|
+
|
|
63
|
+
:param flags:
|
|
64
|
+
:return:
|
|
65
|
+
"""
|
|
66
|
+
if not hasattr(self,"_element_flags"):
|
|
67
|
+
self._element_flags = set()
|
|
68
|
+
|
|
69
|
+
if not flags:
|
|
70
|
+
return
|
|
71
|
+
|
|
72
|
+
self._element_flags.update(set(flags))
|
|
73
|
+
|
|
74
|
+
def has_flag(self,flag:ElementFlag) -> bool:
|
|
75
|
+
"""
|
|
76
|
+
True, if this element has a certain flag
|
|
77
|
+
:param flag:
|
|
78
|
+
:return:
|
|
79
|
+
"""
|
|
80
|
+
return flag in self.element_flags
|
|
81
|
+
|
|
82
|
+
def _normal_init(self,parent:"BaseElement",window):
|
|
83
|
+
"""
|
|
84
|
+
Don't override
|
|
85
|
+
:param parent:
|
|
86
|
+
:param window:
|
|
87
|
+
:return:
|
|
88
|
+
"""
|
|
89
|
+
self.parent = parent
|
|
90
|
+
self.window = window
|
|
91
|
+
|
|
92
|
+
window.register_element(self)
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def _personal_init(self):
|
|
96
|
+
"""
|
|
97
|
+
Use to your liking
|
|
98
|
+
:return:
|
|
99
|
+
"""
|
|
100
|
+
...
|
|
101
|
+
|
|
102
|
+
def _get_value(self) -> any:
|
|
103
|
+
"""
|
|
104
|
+
Returns the value(s) of the Element.
|
|
105
|
+
Override this function.
|
|
106
|
+
:return:
|
|
107
|
+
"""
|
|
108
|
+
return None
|
|
109
|
+
|
|
110
|
+
def set_value(self,val:any):
|
|
111
|
+
"""
|
|
112
|
+
Set the value of the element
|
|
113
|
+
:param val: New value
|
|
114
|
+
:return:
|
|
115
|
+
"""
|
|
116
|
+
pass
|
|
117
|
+
|
|
118
|
+
@property
|
|
119
|
+
def value(self) -> any:
|
|
120
|
+
"""
|
|
121
|
+
Value of the surrounding object.
|
|
122
|
+
Override _get_value to create custom values
|
|
123
|
+
:return:
|
|
124
|
+
"""
|
|
125
|
+
return self._get_value()
|
|
126
|
+
|
|
127
|
+
@value.setter
|
|
128
|
+
def value(self, val):
|
|
129
|
+
self.set_value(val)
|
|
130
|
+
|
|
131
|
+
@property
|
|
132
|
+
def tk_widget(self) ->tk.Widget:
|
|
133
|
+
"""
|
|
134
|
+
This will be used to store all contained Widgets into
|
|
135
|
+
:return:
|
|
136
|
+
"""
|
|
137
|
+
if self.parent is None:
|
|
138
|
+
return self._fake_tk_element
|
|
139
|
+
return self.parent.tk_widget
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
class BaseWidget(BaseElement):
|
|
143
|
+
"""
|
|
144
|
+
Base for every Widget
|
|
145
|
+
"""
|
|
146
|
+
_tk_widget:tk.Widget
|
|
147
|
+
_tk_widget_class:type = None # Class of the connected widget
|
|
148
|
+
_tk_args:tuple = tuple() # args and kwargs to pass to the tk_widget_class when initializing
|
|
149
|
+
_tk_kwargs:dict = dict()
|
|
150
|
+
|
|
151
|
+
_tk_target_value:tk.Variable = None # By default, the value of this is read when fetching the value
|
|
152
|
+
|
|
153
|
+
_insert_kwargs:dict = dict() # kwargs for the packer/grid
|
|
154
|
+
|
|
155
|
+
#_is_container:bool = False # True, if this widget contains other widgets
|
|
156
|
+
_contains:Iterable[Iterable[Self]] = []
|
|
157
|
+
|
|
158
|
+
# @property
|
|
159
|
+
# def _is_container(self) -> bool:
|
|
160
|
+
# return False
|
|
161
|
+
|
|
162
|
+
def __init__(self,key:any=None,tk_args:tuple[any]=tuple(),tk_kwargs:dict[str:any]=None):
|
|
163
|
+
self._tk_args = tk_args
|
|
164
|
+
|
|
165
|
+
if tk_kwargs is None:
|
|
166
|
+
tk_kwargs = dict()
|
|
167
|
+
self._tk_kwargs = tk_kwargs
|
|
168
|
+
|
|
169
|
+
self._insert_kwargs = {"side":tk.LEFT}
|
|
170
|
+
self.key = key
|
|
171
|
+
|
|
172
|
+
def bind_event(self,tk_event:str|Event,key_extention:Union[str,any]=None,key:any=None,key_function:Callable|Iterable[Callable]=None,send_wev:bool=False,send_val:bool=False)->Self:
|
|
173
|
+
"""
|
|
174
|
+
Bind a tk-event onto the underlying tk-widget
|
|
175
|
+
|
|
176
|
+
To just throw the element-key, set key_extention = ""
|
|
177
|
+
|
|
178
|
+
:param tk_event: tkinter event-string. You don't need to add brackets, if your event-text is longer than 1 char
|
|
179
|
+
:param key_extention: Added to the event-key
|
|
180
|
+
:param key: event-key. If None and key_extention is not None, it will be appended onto the element-key
|
|
181
|
+
:param key_function: Called when this event is thrown
|
|
182
|
+
:param send_wev: Send window, event, values to functions
|
|
183
|
+
:param send_val: Send element-value to functions
|
|
184
|
+
:return: Calling element for inline-calls
|
|
185
|
+
"""
|
|
186
|
+
new_key = None
|
|
187
|
+
|
|
188
|
+
if hasattr(tk_event,"value"):
|
|
189
|
+
tk_event = tk_event.value
|
|
190
|
+
|
|
191
|
+
if len(tk_event) > 1 and not tk_event.startswith("<"):
|
|
192
|
+
tk_event = f"<{tk_event}>"
|
|
193
|
+
|
|
194
|
+
match (key_extention is not None,key is not None):
|
|
195
|
+
case (True,True):
|
|
196
|
+
new_key = key + key_extention
|
|
197
|
+
case (False,True):
|
|
198
|
+
new_key = key
|
|
199
|
+
case (True,False):
|
|
200
|
+
new_key = self.key + key_extention
|
|
201
|
+
case (False,False):
|
|
202
|
+
pass
|
|
203
|
+
|
|
204
|
+
temp = self.window.get_event_function(self, new_key, key_function=key_function, key_function_send_wev=send_wev,
|
|
205
|
+
key_function_send_val=send_val)
|
|
206
|
+
|
|
207
|
+
self._tk_widget.bind(
|
|
208
|
+
tk_event,
|
|
209
|
+
temp
|
|
210
|
+
)
|
|
211
|
+
|
|
212
|
+
return self
|
|
213
|
+
|
|
214
|
+
# @property
|
|
215
|
+
# def tk_widget(self) ->tk.Widget:
|
|
216
|
+
# """
|
|
217
|
+
# Returns the tkinter widget connected to this sg-widget
|
|
218
|
+
# :return:
|
|
219
|
+
# """
|
|
220
|
+
# return self._tk_widget
|
|
221
|
+
|
|
222
|
+
def _init_widget_for_inherrit(self,container) -> tk.Widget:
|
|
223
|
+
"""
|
|
224
|
+
For inheritance to change the way the widget is instantiated
|
|
225
|
+
:param container:
|
|
226
|
+
:return:
|
|
227
|
+
"""
|
|
228
|
+
return self._tk_widget_class(container, *self._tk_args, **self._tk_kwargs)
|
|
229
|
+
|
|
230
|
+
def _personal_init_inherit(self):
|
|
231
|
+
"""
|
|
232
|
+
At window initialization before full widget initialization
|
|
233
|
+
:return:
|
|
234
|
+
"""
|
|
235
|
+
pass
|
|
236
|
+
|
|
237
|
+
def _personal_init(self):
|
|
238
|
+
self._personal_init_inherit()
|
|
239
|
+
self._init_widget(self.parent.tk_widget) # Init the contained widgets
|
|
240
|
+
|
|
241
|
+
def _set_tk_target_variable(self,value_type:type=tk.StringVar,kwargs_key:str="textvariable",default_value:any=None):
|
|
242
|
+
"""
|
|
243
|
+
Define a target variable for this widget
|
|
244
|
+
:param value_type: Class of the needed variable
|
|
245
|
+
:param kwargs_key: Key this value will be added into the tk-widget
|
|
246
|
+
:param default_value: Passed to value
|
|
247
|
+
:return:
|
|
248
|
+
"""
|
|
249
|
+
self._tk_target_value = value_type(self.tk_widget,value=default_value)
|
|
250
|
+
self._tk_kwargs[kwargs_key] = self._tk_target_value
|
|
251
|
+
|
|
252
|
+
def _init_widget(self,container:tk.Widget|tk.Tk,mode:Literal["pack","grid"]="pack") -> None:
|
|
253
|
+
"""
|
|
254
|
+
Initialize the widget to the container
|
|
255
|
+
:return:
|
|
256
|
+
"""
|
|
257
|
+
self._tk_widget = self._init_widget_for_inherrit(container)
|
|
258
|
+
|
|
259
|
+
match mode:
|
|
260
|
+
case "pack":
|
|
261
|
+
self._tk_widget.pack(**self._insert_kwargs)
|
|
262
|
+
case "grid":
|
|
263
|
+
self._tk_widget.grid(**self._insert_kwargs)
|
|
264
|
+
|
|
265
|
+
if self.has_flag(ElementFlag.IS_CONTAINER):
|
|
266
|
+
self._init_containing()
|
|
267
|
+
|
|
268
|
+
def _init_containing(self):
|
|
269
|
+
"""
|
|
270
|
+
Initialize all containing widgets
|
|
271
|
+
:return:
|
|
272
|
+
"""
|
|
273
|
+
for i in self._contains:
|
|
274
|
+
line = tk.Frame(self._tk_widget)
|
|
275
|
+
|
|
276
|
+
line_elem = BaseElement()
|
|
277
|
+
line_elem._fake_tk_element = line
|
|
278
|
+
|
|
279
|
+
for k in i:
|
|
280
|
+
k._init(line_elem,self.window)
|
|
281
|
+
|
|
282
|
+
line.grid()
|
|
283
|
+
|
|
284
|
+
def _get_value(self) -> any:
|
|
285
|
+
"""
|
|
286
|
+
This method is used when the value/state of the Widget is read.
|
|
287
|
+
:return:
|
|
288
|
+
"""
|
|
289
|
+
try:
|
|
290
|
+
return self._tk_target_value.get() # Standard target
|
|
291
|
+
except AttributeError: # _tk_target_value isn't used
|
|
292
|
+
return None
|
|
293
|
+
|
|
294
|
+
def set_value(self,val:any):
|
|
295
|
+
try:
|
|
296
|
+
self._tk_target_value.set(val)
|
|
297
|
+
except AttributeError:
|
|
298
|
+
pass
|
|
299
|
+
|
|
300
|
+
class BaseWidgetContainer(BaseWidget):
|
|
301
|
+
"""
|
|
302
|
+
Base for Widgets that contain other widgets
|
|
303
|
+
"""
|
|
304
|
+
def _flag_init(self):
|
|
305
|
+
super()._flag_init()
|
|
306
|
+
self.add_flags(ElementFlag.IS_CONTAINER)
|
|
307
|
+
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
from enum import Enum
|
|
2
|
+
|
|
3
|
+
class ElementFlag(Enum):
|
|
4
|
+
# Flags 0-100 might be used
|
|
5
|
+
IS_CONTAINER = 1 # Element may contain other elements
|
|
6
|
+
DONT_REGISTER_KEY = 2 # Element isn't registered into the window
|
|
7
|
+
|
|
8
|
+
# Flags 100-200 will never be used, so they are available for you to create custom flags
|
|
9
|
+
# It's important to set an actual value for each flag so it can be saved/loaded correctly
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import enum
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class Event(enum.Enum):
|
|
6
|
+
|
|
7
|
+
FocusIn = "FocusIn" # Element got focus in layout
|
|
8
|
+
FocusOut = "FocusOut" #
|
|
9
|
+
|
|
10
|
+
### Mouse ###
|
|
11
|
+
MouseWheel = "MouseWheel" # Scrolled with scroll wheel
|
|
12
|
+
MouseMove = "Motion" # Mouse has been moved
|
|
13
|
+
|
|
14
|
+
MouseEnter = "Enter" # Mouse hovering over the event
|
|
15
|
+
MouseExit = "Leave" #
|
|
16
|
+
|
|
17
|
+
ClickAny = "Button"
|
|
18
|
+
ClickLeft = "Button-1"
|
|
19
|
+
ClickMiddle = "Button-2"
|
|
20
|
+
ClickRight = "Button-3"
|
|
21
|
+
|
|
22
|
+
ClickDoubleAny = "Double-Button"
|
|
23
|
+
ClickDoubleLeft = "Double-Button-1"
|
|
24
|
+
ClickDoubleMiddle = "Double-Button-2"
|
|
25
|
+
ClickDoubleRight = "Double-Button-3"
|
|
26
|
+
|
|
27
|
+
### Special keys ###
|
|
28
|
+
KeyEnter = "Return"
|
|
29
|
+
KeySpace = "Space"
|
|
30
|
+
KeyShift = "Shift"
|
|
31
|
+
KeyAlt = "Alt"
|
|
32
|
+
KeyControl = "Control"
|
|
33
|
+
|
|
34
|
+
### Combinations ###
|
|
35
|
+
Control_Enter = "Control-Return"
|
|
36
|
+
Shift_Enter = "Shift-Return"
|
|
37
|
+
|
|
38
|
+
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
from collections.abc import Callable
|
|
2
|
+
from SwiftGUI import BaseElement, Window
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
# Todo
|
|
6
|
+
### Some useful key-functions to use in your layout
|
|
7
|
+
|
|
8
|
+
### ATTENTION! send_wev has to be true for some of them!
|
|
9
|
+
|
|
10
|
+
def copy_value(to_key:any) -> Callable:
|
|
11
|
+
"""
|
|
12
|
+
Copies the value to the specified key
|
|
13
|
+
:param to_key: Element to copy to
|
|
14
|
+
:return:
|
|
15
|
+
"""
|
|
16
|
+
def fkt(w:Window,e,v,val=NotImplemented):
|
|
17
|
+
if val is NotImplemented:
|
|
18
|
+
val = v[e]
|
|
19
|
+
|
|
20
|
+
w[to_key].set_value(val)
|
|
21
|
+
|
|
22
|
+
return fkt
|
|
23
|
+
|
|
24
|
+
def clear_input_element():
|
|
25
|
+
...
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
from typing import Self
|
|
2
|
+
|
|
3
|
+
All_Keys:dict[str:"Key"] = dict()
|
|
4
|
+
SEPARATOR = "."
|
|
5
|
+
duplicate_warnings:bool = True # Turn this off, if you know that there will be duplicates.
|
|
6
|
+
|
|
7
|
+
class Key:
|
|
8
|
+
def __init__(self,key:str):
|
|
9
|
+
self.key = key
|
|
10
|
+
|
|
11
|
+
def __new__(cls, *args, **kwargs):
|
|
12
|
+
key = args[0]
|
|
13
|
+
|
|
14
|
+
if key in All_Keys:
|
|
15
|
+
|
|
16
|
+
if duplicate_warnings:
|
|
17
|
+
print(f"WARNING! Duplicate key: {key}")
|
|
18
|
+
|
|
19
|
+
return All_Keys[key]
|
|
20
|
+
|
|
21
|
+
temp = super().__new__(cls)
|
|
22
|
+
All_Keys[key] = temp
|
|
23
|
+
|
|
24
|
+
return temp
|
|
25
|
+
|
|
26
|
+
# def __del__(self):
|
|
27
|
+
# if self.key in All_Keys:
|
|
28
|
+
# del All_Keys[self.key]
|
|
29
|
+
|
|
30
|
+
def _add(self,other:Self|str,inverse:bool=False):
|
|
31
|
+
try:
|
|
32
|
+
if inverse:
|
|
33
|
+
return Key(str(other) + SEPARATOR + str(self))
|
|
34
|
+
|
|
35
|
+
return Key(str(self) + SEPARATOR + str(other))
|
|
36
|
+
except AttributeError:
|
|
37
|
+
return NotImplemented
|
|
38
|
+
|
|
39
|
+
def __add__(self, other:Self|str):
|
|
40
|
+
return self._add(other)
|
|
41
|
+
|
|
42
|
+
def __radd__(self, other):
|
|
43
|
+
return self._add(other,inverse=True)
|
|
44
|
+
|
|
45
|
+
def __str__(self):
|
|
46
|
+
return self.key
|
|
47
|
+
|
|
48
|
+
def __hash__(self):
|
|
49
|
+
return hash(self.key)
|
|
50
|
+
|
|
51
|
+
def __eq__(self, other):
|
|
52
|
+
return hash(self) == hash(other)
|
|
53
|
+
|
|
54
|
+
def __repr__(self):
|
|
55
|
+
return str(self)
|
|
56
|
+
|
|
57
|
+
def __contains__(self, item:Self|str):
|
|
58
|
+
"""
|
|
59
|
+
Different way than comparing strings.
|
|
60
|
+
Returns true, if self is any parent-key-group of item.
|
|
61
|
+
:param item:
|
|
62
|
+
:return:
|
|
63
|
+
"""
|
|
64
|
+
return str(self) in str(item)
|
|
65
|
+
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import tkinter as tk
|
|
2
|
+
from collections.abc import Iterable, Callable
|
|
3
|
+
from SwiftGUI import BaseElement, ElementFlag
|
|
4
|
+
from .Base import BaseWidget,BaseWidgetContainer
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
# Todo: Add docstrings to __init__ methods
|
|
8
|
+
|
|
9
|
+
class Example(BaseWidget):
|
|
10
|
+
"""
|
|
11
|
+
Copy this class ot create your own Widget
|
|
12
|
+
|
|
13
|
+
The following methods are to be overwritten if needed:
|
|
14
|
+
_get_value (determines the value returned by this widget)
|
|
15
|
+
_init_widget_for_inherrit (Initializes the widget)
|
|
16
|
+
"""
|
|
17
|
+
_tk_widget_class:type = None # Class of the connected widget
|
|
18
|
+
|
|
19
|
+
def __init__(
|
|
20
|
+
self,
|
|
21
|
+
# Add here
|
|
22
|
+
tk_args:tuple[any]=tuple(),
|
|
23
|
+
tk_kwargs:dict[str:any]=None
|
|
24
|
+
):
|
|
25
|
+
super().__init__(tk_args=tk_args,tk_kwargs=tk_kwargs)
|
|
26
|
+
|
|
27
|
+
if tk_kwargs is None:
|
|
28
|
+
tk_kwargs = dict()
|
|
29
|
+
|
|
30
|
+
self._tk_args = self._tk_args + tk_args # Add anonymous arguments for the widget here
|
|
31
|
+
self._tk_kwargs.update({
|
|
32
|
+
**tk_kwargs
|
|
33
|
+
# Insert named arguments for the widget here
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
class Text(BaseWidget):
|
|
37
|
+
"""
|
|
38
|
+
Copy this class ot create your own Widget
|
|
39
|
+
"""
|
|
40
|
+
_tk_widget_class:type = tk.Label # Class of the connected widget
|
|
41
|
+
|
|
42
|
+
def __init__(
|
|
43
|
+
self,
|
|
44
|
+
# Add here
|
|
45
|
+
text:str = "",
|
|
46
|
+
key:any=None,
|
|
47
|
+
|
|
48
|
+
tk_args:tuple[any]=tuple(),
|
|
49
|
+
tk_kwargs:dict[str:any]=None
|
|
50
|
+
):
|
|
51
|
+
super().__init__(key=key,tk_args=tk_args,tk_kwargs=tk_kwargs)
|
|
52
|
+
|
|
53
|
+
if tk_kwargs is None:
|
|
54
|
+
tk_kwargs = dict()
|
|
55
|
+
|
|
56
|
+
self._tk_kwargs.update({
|
|
57
|
+
**tk_kwargs,
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
self._text = text
|
|
61
|
+
|
|
62
|
+
def _personal_init_inherit(self):
|
|
63
|
+
self._set_tk_target_variable(default_value=self._text)
|
|
64
|
+
|
|
65
|
+
# Aliases
|
|
66
|
+
T = Text
|
|
67
|
+
Label = Text
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
class Frame(BaseWidgetContainer):
|
|
71
|
+
"""
|
|
72
|
+
Copy this class ot create your own Widget
|
|
73
|
+
"""
|
|
74
|
+
_tk_widget_class:type = tk.Frame # Class of the connected widget
|
|
75
|
+
|
|
76
|
+
def __init__(
|
|
77
|
+
self,
|
|
78
|
+
layout:Iterable[Iterable[BaseElement]],
|
|
79
|
+
# Add here
|
|
80
|
+
tk_args:tuple[any]=tuple(),
|
|
81
|
+
tk_kwargs:dict[str:any]=None
|
|
82
|
+
):
|
|
83
|
+
super().__init__(tk_args=tk_args,tk_kwargs=tk_kwargs)
|
|
84
|
+
|
|
85
|
+
self._contains = layout
|
|
86
|
+
|
|
87
|
+
self._tk_args = self._tk_args + tk_args # Add anonymous arguments for the widget here
|
|
88
|
+
if tk_kwargs is None:
|
|
89
|
+
tk_kwargs = dict()
|
|
90
|
+
self._tk_kwargs.update({
|
|
91
|
+
**tk_kwargs
|
|
92
|
+
# Insert named arguments for the widget here
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
def window_entry_point(self,root:tk.Tk|tk.Widget,window:BaseElement):
|
|
96
|
+
"""
|
|
97
|
+
Starting point for the whole window, or part of the layout.
|
|
98
|
+
Don't use this unless you overwrite the sg.Window class
|
|
99
|
+
:param window: Window Element
|
|
100
|
+
:param root: Window to put every element
|
|
101
|
+
:return:
|
|
102
|
+
"""
|
|
103
|
+
self.window = window
|
|
104
|
+
self.add_flags(ElementFlag.IS_CONTAINER)
|
|
105
|
+
self._init_widget(root)
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
# Aliases
|
|
109
|
+
Column = Frame
|
|
110
|
+
|
|
111
|
+
class Button(BaseWidget):
|
|
112
|
+
"""
|
|
113
|
+
Copy this class ot create your own Widget
|
|
114
|
+
|
|
115
|
+
The following methods are to be overwritten if needed:
|
|
116
|
+
_get_value (determines the value returned by this widget)
|
|
117
|
+
_init_widget_for_inherrit (Initializes the widget)
|
|
118
|
+
"""
|
|
119
|
+
_tk_widget_class:type = tk.Button # Class of the connected widget
|
|
120
|
+
|
|
121
|
+
def __init__(
|
|
122
|
+
self,
|
|
123
|
+
# Add here
|
|
124
|
+
text:str = "",
|
|
125
|
+
key:any = None,
|
|
126
|
+
key_function:Callable|Iterable[Callable] = None,
|
|
127
|
+
key_function_send_wev:bool = False,
|
|
128
|
+
tk_args:tuple[any]=tuple(),
|
|
129
|
+
tk_kwargs:dict[str:any]=None
|
|
130
|
+
):
|
|
131
|
+
super().__init__(key=key,tk_args=tk_args,tk_kwargs=tk_kwargs)
|
|
132
|
+
|
|
133
|
+
if tk_kwargs is None:
|
|
134
|
+
tk_kwargs = dict()
|
|
135
|
+
|
|
136
|
+
self._tk_args = self._tk_args + tk_args # Add anonymous arguments for the widget here
|
|
137
|
+
self._tk_kwargs.update({
|
|
138
|
+
**tk_kwargs,
|
|
139
|
+
# Insert named arguments for the widget here
|
|
140
|
+
"text":text,
|
|
141
|
+
})
|
|
142
|
+
#tk.Button(command=)
|
|
143
|
+
|
|
144
|
+
self._key_function = key_function
|
|
145
|
+
self._key_function_send_wev = key_function_send_wev
|
|
146
|
+
|
|
147
|
+
def _personal_init(self):
|
|
148
|
+
self._tk_kwargs.update({
|
|
149
|
+
"command": self.window.get_event_function(self, self.key, self._key_function, self._key_function_send_wev, self._key_function_send_val)
|
|
150
|
+
})
|
|
151
|
+
|
|
152
|
+
super()._personal_init()
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
class Input(BaseWidget):
|
|
156
|
+
"""
|
|
157
|
+
Copy this class ot create your own Widget
|
|
158
|
+
|
|
159
|
+
The following methods are to be overwritten if needed:
|
|
160
|
+
_get_value (determines the value returned by this widget)
|
|
161
|
+
_init_widget_for_inherrit (Initializes the widget)
|
|
162
|
+
"""
|
|
163
|
+
_tk_widget_class:type = tk.Entry # Class of the connected widget
|
|
164
|
+
|
|
165
|
+
def __init__(
|
|
166
|
+
self,
|
|
167
|
+
# Add here
|
|
168
|
+
text:str = "",
|
|
169
|
+
key:any = None,
|
|
170
|
+
key_function:Callable|Iterable[Callable] = None,
|
|
171
|
+
key_function_send_wev:bool = False,
|
|
172
|
+
key_function_send_val:bool = False,
|
|
173
|
+
tk_args:tuple[any]=tuple(),
|
|
174
|
+
tk_kwargs:dict[str:any]=None
|
|
175
|
+
):
|
|
176
|
+
super().__init__(key=key,tk_args=tk_args,tk_kwargs=tk_kwargs)
|
|
177
|
+
|
|
178
|
+
if tk_kwargs is None:
|
|
179
|
+
tk_kwargs = dict()
|
|
180
|
+
|
|
181
|
+
self._tk_args = self._tk_args + tk_args # Add anonymous arguments for the widget here
|
|
182
|
+
self._tk_kwargs.update({
|
|
183
|
+
**tk_kwargs,
|
|
184
|
+
# Insert named arguments for the widget here
|
|
185
|
+
"text":text,
|
|
186
|
+
})
|
|
187
|
+
#tk.Button(command=)
|
|
188
|
+
|
|
189
|
+
self._key_function = key_function
|
|
190
|
+
self._key_function_send_wev = key_function_send_wev
|
|
191
|
+
self._key_function_send_val = key_function_send_val
|
|
192
|
+
|
|
193
|
+
def _personal_init(self):
|
|
194
|
+
self._tk_target_value = tk.StringVar(self.window.tk_widget)
|
|
195
|
+
|
|
196
|
+
self._tk_kwargs.update({
|
|
197
|
+
#"command": self.window.get_event_function(self.key, self._key_function, self._key_function_send_wev),
|
|
198
|
+
"textvariable":self._tk_target_value,
|
|
199
|
+
})
|
|
200
|
+
|
|
201
|
+
super()._personal_init()
|
|
202
|
+
|
|
203
|
+
# Aliases
|
|
204
|
+
In = Input
|
|
205
|
+
Entry = Input
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
from collections.abc import Iterable
|
|
2
|
+
from SwiftGUI import BaseElement, Frame, Text, Input
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
# Advanced / Combined elements
|
|
6
|
+
|
|
7
|
+
class Form(BaseElement):
|
|
8
|
+
"""
|
|
9
|
+
Grid-Layout-Form with text-Input-combinations
|
|
10
|
+
|
|
11
|
+
Still very WIP (of course), just a proof of concept
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
def __init__(
|
|
15
|
+
self,
|
|
16
|
+
texts:Iterable[str],
|
|
17
|
+
key:any = "",
|
|
18
|
+
seperate_keys:bool=False, # Key for every input
|
|
19
|
+
):
|
|
20
|
+
self.key = key
|
|
21
|
+
self.texts = texts
|
|
22
|
+
|
|
23
|
+
self.layout = [
|
|
24
|
+
[
|
|
25
|
+
Text(line),
|
|
26
|
+
Input(
|
|
27
|
+
key=key + line if seperate_keys else None
|
|
28
|
+
),
|
|
29
|
+
] for line in texts
|
|
30
|
+
]
|
|
31
|
+
|
|
32
|
+
self._sg_widget = Frame(self.layout)
|
|
33
|
+
|
|
34
|
+
def _personal_init(self):
|
|
35
|
+
self._sg_widget._init(self,self.window)
|
|
36
|
+
|
|
37
|
+
def _get_value(self) -> any:
|
|
38
|
+
return {
|
|
39
|
+
line:elem[1].value for line,elem in zip(self.texts,self.layout)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
def set_value(self,val:dict[str:str]):
|
|
43
|
+
"""
|
|
44
|
+
Update only passed keys with their value
|
|
45
|
+
:param val:
|
|
46
|
+
:return:
|
|
47
|
+
"""
|
|
48
|
+
for i,text in enumerate(self.texts):
|
|
49
|
+
if text in val.keys():
|
|
50
|
+
self.layout[i][1].value = val[text]
|
|
51
|
+
|
|
52
|
+
def clear_all_values(self):
|
|
53
|
+
"""
|
|
54
|
+
Does what it says
|
|
55
|
+
:return:
|
|
56
|
+
"""
|
|
57
|
+
for i,_ in enumerate(self.texts):
|
|
58
|
+
self.layout[i][1].value = ""
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import tkinter as tk
|
|
2
|
+
from collections.abc import Iterable,Callable
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
from typing import TYPE_CHECKING
|
|
5
|
+
from warnings import deprecated
|
|
6
|
+
|
|
7
|
+
from SwiftGUI import BaseElement, Frame
|
|
8
|
+
|
|
9
|
+
if TYPE_CHECKING:
|
|
10
|
+
from SwiftGUI import AnyElement
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@deprecated("WIP")
|
|
14
|
+
@dataclass
|
|
15
|
+
class Options_Windowwide:
|
|
16
|
+
... # Contains options for all Elements inside a window
|
|
17
|
+
|
|
18
|
+
# Windows-Class
|
|
19
|
+
|
|
20
|
+
class Window(BaseElement):
|
|
21
|
+
_prev_event:any = None # Most recent event (-key)
|
|
22
|
+
values:dict # Key:Value of all named elements
|
|
23
|
+
|
|
24
|
+
all_key_elements: dict[any, "AnyElement"] # Key:Element, if key is present
|
|
25
|
+
|
|
26
|
+
exists:bool = False # True, if this window exists at the moment
|
|
27
|
+
|
|
28
|
+
def __init__(self,layout:Iterable[Iterable[BaseElement]]):
|
|
29
|
+
self.all_elements:list["AnyElement"] = list() # Elemente will be registered in here
|
|
30
|
+
self.all_key_elements:dict[any,"AnyElement"] = dict() # Key:Element, if key is present
|
|
31
|
+
self.values = dict()
|
|
32
|
+
|
|
33
|
+
self._tk = tk.Tk()
|
|
34
|
+
|
|
35
|
+
self._sg_widget:Frame = Frame(layout)
|
|
36
|
+
self._sg_widget.window_entry_point(self._tk, self)
|
|
37
|
+
|
|
38
|
+
self.refresh_values()
|
|
39
|
+
|
|
40
|
+
@property
|
|
41
|
+
def tk_widget(self) ->tk.Widget:
|
|
42
|
+
return self._sg_widget.tk_widget
|
|
43
|
+
|
|
44
|
+
def loop(self) -> tuple[any,dict[any:any]]:
|
|
45
|
+
"""
|
|
46
|
+
Main loop
|
|
47
|
+
|
|
48
|
+
When window is closed, None is returned as the key.
|
|
49
|
+
|
|
50
|
+
:return: Triggering event key; all values as dict
|
|
51
|
+
"""
|
|
52
|
+
self.exists = True
|
|
53
|
+
self._tk.mainloop()
|
|
54
|
+
|
|
55
|
+
try:
|
|
56
|
+
assert self._tk.winfo_exists()
|
|
57
|
+
except (AssertionError,tk.TclError):
|
|
58
|
+
self.exists = False
|
|
59
|
+
return None,self.values
|
|
60
|
+
|
|
61
|
+
return self._prev_event, self.values
|
|
62
|
+
|
|
63
|
+
def register_element(self,elem:BaseElement):
|
|
64
|
+
"""
|
|
65
|
+
Register an Element in this window
|
|
66
|
+
:param elem:
|
|
67
|
+
:return:
|
|
68
|
+
"""
|
|
69
|
+
self.all_elements.append(elem)
|
|
70
|
+
|
|
71
|
+
if elem.key is not None:
|
|
72
|
+
if elem.key in self.all_key_elements:
|
|
73
|
+
print(f"WARNING! Key {elem.key} is defined multiple times!")
|
|
74
|
+
|
|
75
|
+
self.all_key_elements[elem.key] = elem
|
|
76
|
+
|
|
77
|
+
def throw_event(self,key:any,value:any=None):
|
|
78
|
+
"""
|
|
79
|
+
Thread-safe method to generate a custom event.
|
|
80
|
+
|
|
81
|
+
:param key:
|
|
82
|
+
:param value: If not None, it will be saved inside the value-dict until changed
|
|
83
|
+
:return:
|
|
84
|
+
"""
|
|
85
|
+
if value is not None:
|
|
86
|
+
self.values[key] = value
|
|
87
|
+
self._tk.after(0,self._receive_event,key)
|
|
88
|
+
|
|
89
|
+
@deprecated("WIP")
|
|
90
|
+
def throw_event_on_next_loop(self,key:any,value:any=None):
|
|
91
|
+
"""
|
|
92
|
+
NOT THREAD-SAFE!!!
|
|
93
|
+
|
|
94
|
+
Generate an event instantly when window returns to loop
|
|
95
|
+
:param key:
|
|
96
|
+
:param value: If not None, it will be saved inside the value-dict until changed
|
|
97
|
+
:return:
|
|
98
|
+
"""
|
|
99
|
+
# Todo
|
|
100
|
+
...
|
|
101
|
+
|
|
102
|
+
def _receive_event(self,key:any):
|
|
103
|
+
"""
|
|
104
|
+
Gets called when an event is evoked
|
|
105
|
+
:param key:
|
|
106
|
+
:return:
|
|
107
|
+
"""
|
|
108
|
+
self._prev_event = key
|
|
109
|
+
self._tk.quit()
|
|
110
|
+
|
|
111
|
+
def get_event_function(self,me:BaseElement,key:any=None,key_function:Callable|Iterable[Callable]=None,
|
|
112
|
+
key_function_send_wev:bool = False, key_function_send_val:bool = False,
|
|
113
|
+
)->Callable:
|
|
114
|
+
"""
|
|
115
|
+
Returns a function that sets the event-variable accorting to key
|
|
116
|
+
:param key_function_send_val: True, if the element-value should be passed to the function
|
|
117
|
+
:param me: Calling element
|
|
118
|
+
:param key_function_send_wev: True, if additional_function should be called with window, event, value as argument
|
|
119
|
+
:param key_function: Will be called additionally to the event. YOU CAN PASS MULTIPLE FUNCTIONS as a list/tuple
|
|
120
|
+
:param key: If passed, main loop will return this key
|
|
121
|
+
:return: Function to use as a tk-event
|
|
122
|
+
"""
|
|
123
|
+
if (key_function is not None) and not hasattr(key_function, "__iter__"):
|
|
124
|
+
key_function = (key_function,)
|
|
125
|
+
|
|
126
|
+
def single_event(*_):
|
|
127
|
+
self.refresh_values()
|
|
128
|
+
|
|
129
|
+
if key_function: # Call key-functions
|
|
130
|
+
for fkt in key_function:
|
|
131
|
+
args = list()
|
|
132
|
+
|
|
133
|
+
if key_function_send_wev:
|
|
134
|
+
args.extend((self,key,self.values))
|
|
135
|
+
|
|
136
|
+
if key_function_send_val:
|
|
137
|
+
args.append(me.value)
|
|
138
|
+
|
|
139
|
+
fkt(*args)
|
|
140
|
+
|
|
141
|
+
self.refresh_values() # In case you change values with the key-functions
|
|
142
|
+
|
|
143
|
+
if key is not None: # Call named event
|
|
144
|
+
self._receive_event(key)
|
|
145
|
+
|
|
146
|
+
return single_event
|
|
147
|
+
|
|
148
|
+
def refresh_values(self) -> dict:
|
|
149
|
+
"""
|
|
150
|
+
"Picks up" all values from the elements to store them in Window.values
|
|
151
|
+
:return: new values
|
|
152
|
+
"""
|
|
153
|
+
for key,elem in self.all_key_elements.items():
|
|
154
|
+
self.values[key] = elem.value
|
|
155
|
+
|
|
156
|
+
return self.values
|
|
157
|
+
|
|
158
|
+
def __getitem__(self, item) -> "AnyElement":
|
|
159
|
+
try:
|
|
160
|
+
return self.all_key_elements[item]
|
|
161
|
+
except KeyError:
|
|
162
|
+
raise KeyError(f"The requested Element ({item}) wasn't found. Did you forget to set its key?")
|