pygui_simple 0.4.0__tar.gz → 0.5.0__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pygui_simple
3
- Version: 0.4.0
3
+ Version: 0.5.0
4
4
  Summary: A simple gui library
5
5
  Author: Youchee
6
6
  Author-email: Youchee <youchi.xu@live.com>
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "pygui_simple"
3
- version = "0.4.0"
3
+ version = "0.5.0"
4
4
  description = "A simple gui library"
5
5
  readme = "README.md"
6
6
  authors = [
@@ -3,7 +3,10 @@
3
3
  from dataclasses import dataclass, field
4
4
  import tkinter as tk
5
5
  from typing import cast, Any
6
+ from typing import TypeAlias
7
+ from collections.abc import Callable
6
8
 
9
+ from matplotlib import backend_bases
7
10
  from matplotlib.figure import Figure
8
11
  from matplotlib.lines import Line2D
9
12
  from matplotlib.container import Container
@@ -16,7 +19,7 @@ import numpy as np
16
19
  from pygui_simple.tkcontrol import tkControl
17
20
 
18
21
 
19
- __version__ = "2.4.0"
22
+ __version__ = "2.5.0"
20
23
 
21
24
 
22
25
  font = {'family' : 'SimHei',
@@ -34,6 +37,7 @@ class LineData:
34
37
  visible: bool = True
35
38
  line: Line2D | Container | None = None
36
39
 
40
+ Event_Callback: TypeAlias = Callable[[backend_bases.Event], object]
37
41
 
38
42
  class MatPlotCtrl(tkControl):
39
43
  def __init__(
@@ -61,11 +65,11 @@ class MatPlotCtrl(tkControl):
61
65
  self._xlabel: str = xlabel
62
66
  self._ylabel: str = ylabel
63
67
 
64
- fig: Figure = Figure(figsize=self._size, dpi=self._dpi)
65
- self._canvas: FigureCanvasTkAgg = FigureCanvasTkAgg(fig, parent)
68
+ self._fig: Figure = Figure(figsize=self._size, dpi=self._dpi)
69
+ self._canvas: FigureCanvasTkAgg = FigureCanvasTkAgg(self._fig, parent)
66
70
  super().__init__(parent, title, idself, self._canvas.get_tk_widget())
67
- self._ax: Axes = fig.add_subplot()
68
- fig.subplots_adjust(bottom=0.25)
71
+ self._ax: Axes = self._fig.add_subplot()
72
+ self._fig.subplots_adjust(bottom=0.25)
69
73
  self._create()
70
74
 
71
75
  def _create(self):
@@ -84,12 +88,32 @@ class MatPlotCtrl(tkControl):
84
88
  def xdata(self, xdata: ArrayLike):
85
89
  self._xdata = xdata
86
90
 
91
+ @property
92
+ def ax(self):
93
+ return self._ax
94
+
95
+ @property
96
+ def canvas(self):
97
+ return self._canvas
98
+
99
+ def add_tooltip(self, x: float,
100
+ y: float,
101
+ s: str,
102
+ fontdict: dict[str, Any] | None = None,
103
+ **kwargs: Any):
104
+ return self._ax.text(x, y, s, fontdict, **kwargs)
105
+
106
+ def event_callback(self, s: str, func: Event_Callback):
107
+ _ = self._fig.canvas.mpl_connect(s, func)
108
+
87
109
  def _bar(self, xdata: ArrayLike, ydata: ArrayLike, **style_dict: Any):
88
110
  bar = self._ax.bar(xdata, ydata, **style_dict)
89
- if isinstance(bar, list):
90
- return bar[0]
91
- else:
92
- return bar
111
+ # if isinstance(bar, list):
112
+ # return bar[0]
113
+ # else:
114
+ # return bar
115
+
116
+ return bar
93
117
 
94
118
  def _plot(self, xdata: ArrayLike, ydata: ArrayLike, **style_dict: Any):
95
119
  line = self._ax.plot(xdata, ydata, **style_dict)
@@ -9,7 +9,7 @@ import platform
9
9
  import ctypes
10
10
  from functools import partial
11
11
  # from collections import OrderedDict
12
- from typing import Literal, Any, override, cast, Unpack
12
+ from typing import Literal, Any, override, cast, TypeAlias
13
13
  from typing import Protocol, TypeVar, Generic
14
14
  # from typing import get_args, get_origin
15
15
  # from collections.abc import Callable
@@ -628,6 +628,162 @@ class ToolbarCtrl(tkControl):
628
628
  super().destroy()
629
629
 
630
630
 
631
+ _FontDescription: TypeAlias = str | tuple[str, int] | tuple[str, int, str]
632
+
633
+ class CanvasCtrl(tkControl):
634
+ def __init__(self, parent: tk.Misc, idself: str, res_path: str = "", **options: Any):
635
+ ctrl = tk.Canvas(parent, **options)
636
+ tkControl.__init__(self, parent, "", idself, ctrl)
637
+ self._res_path: str = res_path
638
+ self._img_cache: list[ImageTk.PhotoImage | None] = []
639
+
640
+ def create_text(
641
+ self,
642
+ x: float,
643
+ y: float,
644
+ /,
645
+ *,
646
+ activefill: str = "",
647
+ activestipple: str = "",
648
+ anchor: Literal['nw', 'n', 'ne', 'w', 'center', 'e', 'sw', 's', 'se'] = "center",
649
+ angle: float | str = 0,
650
+ disabledfill: str = "",
651
+ disabledstipple: str = "",
652
+ fill: str = "black",
653
+ font: _FontDescription = "",
654
+ justify: Literal['left', 'center', 'right'] = "center",
655
+ offset: float | str = "n",
656
+ state: Literal['normal', 'hidden', 'disabled'] = "normal",
657
+ stipple: str = "",
658
+ tags: str | list[str] | tuple[str, ...] = "",
659
+ text: float | str = "",
660
+ width: float | str = 0
661
+ ) -> int:
662
+ """Typed wrapper for tk.Canvas.create_text with positional-only x/y and keyword-only remaining args.
663
+
664
+ Args:
665
+ x: Horizontal coordinate for text anchor point, positional-only parameter.
666
+ y: Vertical coordinate for text anchor point, positional-only parameter.
667
+ activefill: Fill color when item is in active state.
668
+ activestipple: Stipple pattern for active fill color.
669
+ anchor: Anchor position aligning text to (x,y) coordinate.
670
+ angle: Rotation angle of text; None uses Tk internal default.
671
+ disabledfill: Fill color when item is disabled.
672
+ disabledstipple: Stipple pattern for disabled fill color.
673
+ fill: Normal text foreground color, default set to black for visibility.
674
+ font: Tk font definition (string or font tuple format), None uses default system font.
675
+ justify: Multi-line text alignment mode.
676
+ offset: Text drawing offset value, None uses default.
677
+ state: Display state of text item(normal/hidden/disabled).
678
+ stipple: Stipple pattern applied to text fill.
679
+ tags: Tag label or tag list bound to this canvas item.
680
+ text: Content string or numeric value to render as text.
681
+ width: Max wrap width for multi-line text, None disables auto line wrap.
682
+
683
+ Returns:
684
+ int: Unique integer canvas item ID returned by native Tk canvas.
685
+ """
686
+ canvas = cast(tk.Canvas, self._tkctrl)
687
+ item_id = canvas.create_text(
688
+ x, y,
689
+ activefill=activefill,
690
+ activestipple=activestipple,
691
+ anchor=anchor,
692
+ angle=angle,
693
+ disabledfill=disabledfill,
694
+ disabledstipple=disabledstipple,
695
+ fill=fill,
696
+ font=font,
697
+ justify=justify,
698
+ offset=offset,
699
+ state=state,
700
+ stipple=stipple,
701
+ tags=tags,
702
+ text=text,
703
+ width=width
704
+ )
705
+ return item_id
706
+
707
+ def create_rectangle(self,
708
+ x1: float,
709
+ y1: float,
710
+ x2: float,
711
+ y2: float,
712
+ /,
713
+ *,
714
+ fill: str = "",
715
+ outline: str = "",
716
+ width: float | str = 1,
717
+ dash: str | int | list[int] | tuple[int, ...] = None,
718
+ stipple: str = "",
719
+ activefill: str = "",
720
+ activeoutline: str = "",
721
+ activedash: str | int | list[int] | tuple[int, ...] = None,
722
+ disabledfill: str = "",
723
+ disabledoutline: str = "",
724
+ state: Literal['normal', 'hidden', 'disabled'] = "normal",
725
+ tags: str | list[str] | tuple[str, ...] = ""
726
+ ) -> int:
727
+ canvas = cast(tk.Canvas, self._tkctrl)
728
+ item_id = canvas.create_rectangle(
729
+ x1, y1, x2, y2,
730
+ fill=fill,
731
+ outline=outline,
732
+ width=width,
733
+ dash=dash,
734
+ stipple=stipple,
735
+ activefill=activefill,
736
+ activeoutline=activeoutline,
737
+ activedash=activedash,
738
+ disabledfill=disabledfill,
739
+ disabledoutline=disabledoutline,
740
+ state=state,
741
+ tags=tags
742
+ )
743
+ return item_id
744
+
745
+ def create_image(
746
+ self,
747
+ x: float,
748
+ y: float,
749
+ /,
750
+ *,
751
+ file_path: str,
752
+ target_w: int,
753
+ target_h: int,
754
+ anchor: Literal['nw', 'n', 'ne', 'w', 'center', 'e', 'sw', 's', 'se'] = "center",
755
+ fallback_fill: str = "#cccccc",
756
+ tags: str | list[str] | tuple[str, ...] | None = None
757
+ ) -> int:
758
+ """Load local image file, auto resize then draw on canvas; auto fallback to colored rectangle on load failure.
759
+ All loaded PhotoImage cached inside instance list to avoid Python GC destroy image resource.
760
+
761
+ Args:
762
+ x: Anchor X coordinate for image placement, positional-only param.
763
+ y: Anchor Y coordinate for image placement, positional-only param.
764
+ file_path: Relative/absolute local path of target image file(png/jpg supported).
765
+ target_w: Fixed pixel width to resize source image.
766
+ target_h: Fixed pixel height to resize source image.
767
+ anchor: Align rule of image against input (x,y) coordinate.
768
+ fallback_fill: Fill color for substitute rectangle when image file missing/corrupted.
769
+ tags: Tag labels bind to generated canvas image item.
770
+
771
+ Returns:
772
+ int: Canvas item ID of either loaded image or fallback substitute rectangle.
773
+ """
774
+ canvas = cast(tk.Canvas, self._tkctrl)
775
+ # Open source image and convert to RGBA to support transparent alpha channel
776
+ image_path = os.path.join(self._res_path, file_path)
777
+ raw_img = Image.open(image_path).convert("RGBA")
778
+ # High quality down/up scale via LANCZOS resample algorithm
779
+ resized_img = raw_img.resize((target_w, target_h), Image.Resampling.LANCZOS)
780
+ photo_obj = ImageTk.PhotoImage(resized_img)
781
+ # Persist image reference into internal cache to prevent garbage collection
782
+ self._img_cache.append(photo_obj)
783
+ item_id = canvas.create_image(x, y, image=photo_obj, anchor=anchor, tags=tags)
784
+ return item_id
785
+
786
+
631
787
  T = TypeVar("T", bound=tk.Tk | tk.Toplevel)
632
788
  class tkWM(Generic[T], Widget):
633
789
  def __init__(self, wm: T):
@@ -1014,7 +1170,8 @@ class tkWin(WinBasic):
1014
1170
  ctrl = ButtonCtrl(master, owner, idctrl, text, **options)
1015
1171
  # TODO
1016
1172
  case "Canvas":
1017
- ctrl = tk.Canvas(master, **options)
1173
+ # ctrl = tk.Canvas(master, **options)
1174
+ ctrl = CanvasCtrl(master, idctrl, self._res_path, **options)
1018
1175
  case "Combobox":
1019
1176
  ctrl = ComboboxCtrl(master, owner,
1020
1177
  idself=idctrl,
File without changes
File without changes