pygui_simple 0.1.0__py3-none-any.whl

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.
@@ -0,0 +1,327 @@
1
+ #!/usr/bin/python3
2
+ # -*- coding: UTF-8 -*-
3
+ from __future__ import annotations
4
+ import abc
5
+ import xml.etree.ElementTree as et
6
+ from typing import cast, Literal, Protocol, override
7
+ # from typing import TypeAlias
8
+ # from collections.abc import Callable, Mapping
9
+ from collections import OrderedDict
10
+
11
+ # from src.pygui.logit import pv, pe, po
12
+
13
+ # EventHanlder: TypeAlias = Callable[[tuple[Any, ...], dict[str, Any]], Any]
14
+ # EventHanlder: TypeAlias = Callable[[dict[str, Any]], Any]
15
+ # EventHanlder: TypeAlias = Callable[[Mapping[str, Any]], Any]
16
+ class EventHanlder(Protocol):
17
+ def __call__(self, **kwargs: object) -> object: ...
18
+
19
+
20
+ # EventsHanlder: TypeAlias = Callable[[str, dict[str, Any]], Any]
21
+ # EventsHanlder: TypeAlias = Callable[[str, Mapping[str, Any]], Any]
22
+ class EventsHanlder(Protocol):
23
+ def __call__(self, idmsg: str, **kwargs: object) -> object: ...
24
+
25
+
26
+ class Widget:
27
+ def __init__(self):
28
+ pass
29
+
30
+ # @abc.abstractmethod
31
+ # def process_message(self, idmsg: str, **kwargs: object) -> object:
32
+ # pass
33
+
34
+
35
+ class Control(Widget, metaclass=abc.ABCMeta):
36
+ def __init__(self, title: str, idself: str):
37
+ super().__init__()
38
+ self._title: str = title
39
+ self._idself: str = idself
40
+ self._backed: bool = True
41
+
42
+ @property
43
+ def title(self):
44
+ return self._title
45
+
46
+ @property
47
+ def backed(self):
48
+ return self._backed
49
+
50
+ def back(self, bset: bool = True):
51
+ self._backed = bset
52
+
53
+ @abc.abstractmethod
54
+ def configure(self, **kwargs: object):
55
+ pass
56
+
57
+ @abc.abstractmethod
58
+ def __getitem__(self, item: str):
59
+ pass
60
+
61
+ @abc.abstractmethod
62
+ def __setitem__(self, item: str, value: object):
63
+ pass
64
+
65
+ @abc.abstractmethod
66
+ def hide(self, is_hide: bool = True):
67
+ pass
68
+
69
+ def show(self):
70
+ self.hide(False)
71
+
72
+ @abc.abstractmethod
73
+ def disable(self, is_disbl: bool = True):
74
+ pass
75
+
76
+ def enable(self):
77
+ self.disable(False)
78
+
79
+ @abc.abstractmethod
80
+ def destroy(self):
81
+ pass
82
+
83
+
84
+ class Container(Widget):
85
+ def __init__(self, owner: Container | None = None):
86
+ super().__init__()
87
+ self._eventhandler_dict: dict[str, list[EventHanlder]] = {}
88
+ self._msgs_hanlders: list[tuple[int, list[str], EventsHanlder]] = []
89
+ self._owner: Container | None = owner
90
+
91
+ @property
92
+ def owner(self):
93
+ return self._owner
94
+
95
+ @owner.setter
96
+ def owner(self, val: "Container"):
97
+ self._owner = val
98
+
99
+ def register_eventhandler(self, idmsg: str, handler: EventHanlder):
100
+ """_summary_
101
+
102
+ Args:
103
+ idmsg (str): _description_
104
+ handler (EventHanlder): _description_
105
+ """
106
+ handlerlist = self._eventhandler_dict.get(idmsg)
107
+ if handlerlist is not None:
108
+ self._eventhandler_dict[idmsg].append(handler)
109
+ else:
110
+ self._eventhandler_dict[idmsg] = [handler]
111
+ # pv(self._eventhandler_dict)
112
+
113
+ def filter_message(self, hanlder: EventsHanlder,
114
+ typ: Literal[-1, 0, 1] = 0, msglst: list[str] | None = None):
115
+ """filter message to deal with
116
+
117
+ Args:
118
+ hanlder (): hanlder to deal message
119
+ typ ():
120
+ 1: filter all message in msglst
121
+ 0: all message
122
+ -1: filter any message except in msglst
123
+ msglst(): message list
124
+ """
125
+ if msglst is not None:
126
+ self._msgs_hanlders.append((typ, msglst, hanlder))
127
+ else:
128
+ self._msgs_hanlders.append((typ, [], hanlder))
129
+ # pv(self._msgs_hanlders)
130
+
131
+ def process_message(self, idmsg: str, **kwargs: object) -> object:
132
+ for typ, msglst, hander in self._msgs_hanlders:
133
+ if typ == 0:
134
+ ret = hander(idmsg, **kwargs)
135
+ if ret is not None:
136
+ return ret
137
+ elif typ == 1:
138
+ if idmsg in msglst:
139
+ ret = hander(idmsg, **kwargs)
140
+ if ret is not None:
141
+ return ret
142
+ elif typ == -1:
143
+ if idmsg not in msglst:
144
+ ret = hander(idmsg, **kwargs)
145
+ if ret is not None:
146
+ return ret
147
+
148
+ funcs = self._eventhandler_dict.get(idmsg, None)
149
+ if funcs is not None:
150
+ ret = None
151
+ for func in funcs:
152
+ ret = func(**kwargs)
153
+ return ret
154
+ # pv(self._eventhandler_dict)
155
+
156
+ if self._owner is not None:
157
+ return self._owner.process_message(idmsg, **kwargs)
158
+ # po(f"undeal msg of {idmsg}: {kwargs}")
159
+ return None
160
+
161
+
162
+ class Dialog(Container, metaclass=abc.ABCMeta):
163
+ def __init__(self, title: str, width: int, height: int):
164
+ super().__init__()
165
+ self._xx: int = 0
166
+ self._yy: int = 0
167
+ self._title: str = title
168
+ self._ww: int = width
169
+ self._hh: int = height
170
+ self._backed: bool = True
171
+ self._idctrl_dict: OrderedDict[str, Widget] = OrderedDict()
172
+
173
+ @property
174
+ def title(self):
175
+ return self._title
176
+
177
+ def set_title(self, val: str):
178
+ self._title = val
179
+
180
+ @property
181
+ def pos(self):
182
+ return self._xx, self._yy
183
+
184
+ @property
185
+ def size(self):
186
+ return self._ww, self._hh
187
+
188
+ @property
189
+ def backed(self):
190
+ return self._backed
191
+
192
+ def back(self, bset: bool = True):
193
+ for idctrl, ctrl in self._idctrl_dict.items():
194
+ try:
195
+ ctrl = cast(Control, ctrl).back(bset)
196
+ except AttributeError as _:
197
+ # po(f"{idctrl} doesn't support back")
198
+ print(f"{idctrl} doesn't support back")
199
+ self._backed = bset
200
+
201
+ def _beforego(self, **kwargs: object):
202
+ _ = super().process_message("beforego", **kwargs)
203
+
204
+ def _confirm(self, **kwargs: object) -> tuple[bool, str]:
205
+ return cast(tuple[bool, str], super().process_message("confirm", **kwargs))
206
+
207
+ def _cancel(self, **kwargs: object) -> tuple[bool, str]:
208
+ return cast(tuple[bool, str], super().process_message("cancel", **kwargs))
209
+
210
+ @override
211
+ def process_message(self, idmsg: str, **kwargs: object):
212
+ match idmsg:
213
+ case "beforego":
214
+ self._beforego(**kwargs)
215
+ case "confirm":
216
+ return self._confirm(**kwargs)
217
+ case "cancel":
218
+ return self._cancel(**kwargs)
219
+ case _:
220
+ # if self._owner is not None:
221
+ # return self._owner.process_message(idmsg, **kwargs)
222
+ return super().process_message(idmsg, **kwargs)
223
+ return True
224
+
225
+ @abc.abstractmethod
226
+ def destroy(self, **kwargs: object):
227
+ # self._eventhandler_dict.clear()
228
+ # self._eventhandler_dict = {}
229
+ # self._msgs_hanlders.clear()
230
+ # self._msgs_hanlders = []
231
+ pass
232
+
233
+
234
+ class WinBasic(Dialog, metaclass=abc.ABCMeta):
235
+ def __init__(self, xmlfile: str):
236
+ element_tree = et.parse(xmlfile)
237
+ self._wincfg: et.Element[str] = element_tree.getroot()
238
+ win_attr = self._wincfg.attrib
239
+ if "Width" in win_attr:
240
+ w, h = int(win_attr["Width"]), int(win_attr["Height"])
241
+ else:
242
+ w, h = 0, 0
243
+ Dialog.__init__(self, win_attr["Title"], w, h)
244
+
245
+ self._customctrl_dict: dict[str, et.Element] = {}
246
+
247
+ @abc.abstractmethod
248
+ def create_window(self):
249
+ pass
250
+
251
+ def create_xml(self, tag: str, attr_dict: dict[str, str], root: et.Element | None = None) -> et.Element:
252
+ if root is not None:
253
+ item_xml = et.SubElement(root, tag)
254
+ else:
255
+ item_xml = et.Element(tag)
256
+ item_xml.attrib = attr_dict.copy()
257
+ return item_xml
258
+
259
+ @abc.abstractmethod
260
+ def create_control(self, parent: Widget, ctrl_cfg: et.Element,
261
+ level: int = 0, owner: Dialog | None = None) -> tuple[str, Widget]:
262
+ pass
263
+
264
+ @abc.abstractmethod
265
+ def assemble_control(self, ctrl: Widget, attr_dict: dict[str, str], prefix: str = ""):
266
+ pass
267
+
268
+ def create_controls(self, parent: Widget, ctrl_cfg: et.Element,
269
+ level: int = 0, owner: Dialog | None = None) -> OrderedDict[str, Widget]:
270
+ idctrl_dict: OrderedDict[str, Widget] = OrderedDict()
271
+ tag = ctrl_cfg.tag
272
+ if tag == "CustomDialog":
273
+ attr_dict = ctrl_cfg.attrib
274
+ idctrl = attr_dict["id"]
275
+ self._customctrl_dict[idctrl] = ctrl_cfg
276
+ return idctrl_dict
277
+ idctrl, ctrl = self.create_control(parent, ctrl_cfg, level, owner)
278
+ idctrl_dict[idctrl] = ctrl
279
+ if tag in ["Menu", "Notebook", "RadiobuttonGroup",
280
+ "Statusbar", "Toolbar", "Dialog"]:
281
+ pass
282
+ else:
283
+ for subctrl_cfg in list(ctrl_cfg):
284
+ subidctrl_dict = self.create_controls(ctrl, subctrl_cfg, level + 1, owner)
285
+ idctrl_dict.update(subidctrl_dict)
286
+ self.assemble_control(ctrl, ctrl_cfg.attrib, f"{' '*level}")
287
+ return idctrl_dict
288
+
289
+ def delete_control(self, idctrl: str):
290
+ # if idctrl in self._idctrl_dict:
291
+ ctrl = cast(Control, self._idctrl_dict[idctrl])
292
+ try:
293
+ ctrl.destroy()
294
+ except AttributeError:
295
+ # po(f"Warnning: {idctrl} doesn't have destroy")
296
+ print(f"Warnning: {idctrl} doesn't have destroy")
297
+ del self._idctrl_dict[idctrl]
298
+
299
+ def get_control(self, idctrl: str):
300
+ return self._idctrl_dict[idctrl]
301
+
302
+ def get_customctrlcfg(self, idctrl: str):
303
+ return self._customctrl_dict[idctrl]
304
+
305
+ # def register_customctrl(self, idctrl: str, ctrl: Widget):
306
+ # if idctrl in self._idctrl_dict:
307
+ # raise KeyError(f"duplicate key: {idctrl} in idctrl dict")
308
+ # self._idctrl_dict[idctrl] = ctrl
309
+
310
+ @abc.abstractmethod
311
+ def show_err(self, title: str = "", message: str = ""):
312
+ pass
313
+
314
+ @abc.abstractmethod
315
+ def go(self):
316
+ pass
317
+
318
+ @override
319
+ def destroy(self, **kwargs: object):
320
+ # pv(self._idctrl_dict.keys())
321
+ for idctrl in reversed(list(self._idctrl_dict.keys())):
322
+ if idctrl in self._idctrl_dict:
323
+ # po(f"Going to delete {idctrl}")
324
+ self.delete_control(idctrl)
325
+ self._idctrl_dict.clear()
326
+
327
+ super().destroy(**kwargs)
@@ -0,0 +1,17 @@
1
+ Metadata-Version: 2.4
2
+ Name: pygui_simple
3
+ Version: 0.1.0
4
+ Summary: A simple gui library
5
+ Author: Youchee
6
+ Author-email: Youchee <youchi.xu@live.com>
7
+ License-Expression: MIT
8
+ License-File: LICENSE
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: Operating System :: OS Independent
11
+ Requires-Dist: matplotlib>=3.10.8
12
+ Requires-Dist: opencv-python-headless>=4.13.0.92
13
+ Requires-Python: >=3.13
14
+ Project-URL: Homepage, https://github.com/YoucheeXu/pygui
15
+ Project-URL: Issues, https://github.com/YoucheeXu/pygui/issues
16
+ Description-Content-Type: text/markdown
17
+
@@ -0,0 +1,14 @@
1
+ pygui_simple/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
+ pygui_simple/cv2_utilities.py,sha256=EVzD-kMD1XwZGTYUvs1rhfRWVKdT6oBK623xopQuGe0,7543
3
+ pygui_simple/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
+ pygui_simple/tkcalendar.py,sha256=Ig9X02DSNxslDNktK5hadxk1dYcD3HKIUWqetYnemkw,25361
5
+ pygui_simple/tkcontrol.py,sha256=wHDiMmN8BeMS1OqJDhL10lwZzWXdiKhAr80U9t92Vfw,3870
6
+ pygui_simple/tkmatplot.py,sha256=Z8q7X-o868EyuNbGrMyxQzUVwZ5UusTMchj7sOpAqMc,9536
7
+ pygui_simple/tkscrollpicker.py,sha256=eSDL9UhbUZj2qBsgRxnQy52k4yF_a712r8O1_SqWdbM,23581
8
+ pygui_simple/tkslideswitch.py,sha256=orUGQHs3RKJSPE8eeOREEiNAdflwrT6p9izzjldN8r0,16921
9
+ pygui_simple/tkwin.py,sha256=R_tffhoxQPI4GQNga-Z70UvRT3NE29xW5o5jYjpoIXg,54234
10
+ pygui_simple/winbasic.py,sha256=KpWg5Rwn4vmrcwYziY0wsUEHtyAgnyQKXwtuemTyuvM,10620
11
+ pygui_simple-0.1.0.dist-info/licenses/LICENSE,sha256=53fD35OJIw4aBhSn2l89mHYlsxr_33bYstL7IkdtUFQ,1087
12
+ pygui_simple-0.1.0.dist-info/WHEEL,sha256=iHtWm8nRfs0VRdCYVXocAWFW8ppjHL-uTJkAdZJKOBM,80
13
+ pygui_simple-0.1.0.dist-info/METADATA,sha256=19i4F3yg-5ib85pQ69u3WJfgl4ZcFkEVBWkfBclmg0g,555
14
+ pygui_simple-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: uv 0.9.30
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 YoucheeXu
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.