-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathphoto.py
62 lines (52 loc) · 2.46 KB
/
photo.py
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
from base64 import b64decode as _b64decode
from io import BytesIO as _BytesIO
from tkinter import Misc as _Misc, Event as _Event
from typing import Callable as _Callable, override as _override
from PIL.Image import Image as _Image, open as _open
from PIL.ImageTk import PhotoImage as _PhotoImage
from customtkinter import Variable as _Variable, StringVar as _StringVar
from leads_gui.prototype import CanvasBased, VariableControlled
from leads_gui.types import Color as _Color
class ImageVariable(_Variable):
def __init__(self, master: _Misc, image: _Image | None, name: str | None = None) -> None:
super().__init__(master, False, name)
self._image: _Image | None = image
@_override
def set(self, value: _Image | None) -> None:
super().set(not super().get())
self._image = value
@_override
def get(self) -> _Image | None:
return self._image
class Photo(CanvasBased, VariableControlled):
def __init__(self,
master: _Misc,
theme_key: str = "CTkLabel",
width: float = 0,
height: float = 0,
variable: _StringVar | ImageVariable | None = None,
fg_color: _Color | None = None,
hover_color: _Color | None = None,
bg_color: _Color | None = None,
corner_radius: float | None = None,
clickable: bool = False,
command: _Callable[[_Event], None] = lambda _: None) -> None:
CanvasBased.__init__(self, master, theme_key, width, height, fg_color, hover_color, bg_color, corner_radius,
clickable, command)
VariableControlled.__init__(self, variable if variable else _StringVar(master))
self.attach(self.partially_render)
self._image: _PhotoImage | None = None
@_override
def dynamic_renderer(self, canvas: CanvasBased) -> None:
canvas.clear("d")
w, h, hc, vc, limit = canvas.meta()
if image := self._variable.get():
if isinstance(image, str):
image = _open(_BytesIO(_b64decode(image)))
self._image = _PhotoImage(image.resize((int(w), int(h))))
canvas.collect("d0", canvas.create_image(hc, vc, image=self._image))
@_override
def raw_renderer(self, canvas: CanvasBased) -> None:
canvas.clear()
canvas.draw_fg(self._fg_color, self._hover_color, self._corner_radius)
self.dynamic_renderer(canvas)