171 lines
7.5 KiB
Python
171 lines
7.5 KiB
Python
import tkinter as tk
|
|
from math import sin, cos, pi
|
|
|
|
try:
|
|
from PIL import Image, ImageDraw, ImageTk
|
|
PIL_AVAILABLE = True
|
|
except ImportError:
|
|
PIL_AVAILABLE = False
|
|
|
|
def _hex_to_rgb(hex_color):
|
|
hex_color = hex_color.lstrip('#')
|
|
return tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4))
|
|
|
|
class AnimatedIcon(tk.Canvas):
|
|
def __init__(self, master, width=20, height=20, animation_type="counter_arc", color="#2a6fde", highlight_color="#5195ff", use_pillow=False, bg=None):
|
|
if bg is None:
|
|
try:
|
|
bg = master.cget("background")
|
|
except tk.TclError:
|
|
bg = "#f0f0f0" # Fallback color
|
|
super().__init__(master, width=width, height=height, bg=bg, highlightthickness=0)
|
|
|
|
self.width = width
|
|
self.height = height
|
|
self.animation_type = animation_type
|
|
self.color = color
|
|
self.highlight_color = highlight_color
|
|
self.use_pillow = use_pillow and PIL_AVAILABLE
|
|
self.running = False
|
|
self.angle = 0
|
|
|
|
self.color_rgb = _hex_to_rgb(self.color)
|
|
self.highlight_color_rgb = _hex_to_rgb(self.highlight_color)
|
|
|
|
if self.use_pillow:
|
|
self.image = Image.new("RGBA", (width * 4, height * 4), (0, 0, 0, 0))
|
|
self.draw = ImageDraw.Draw(self.image)
|
|
self.photo_image = None
|
|
|
|
def _draw_frame(self):
|
|
if self.use_pillow:
|
|
self._draw_pillow_frame()
|
|
else:
|
|
self._draw_canvas_frame()
|
|
|
|
def _draw_canvas_frame(self):
|
|
self.delete("all")
|
|
if self.animation_type == "line":
|
|
self._draw_canvas_line()
|
|
elif self.animation_type == "double_arc":
|
|
self._draw_canvas_double_arc()
|
|
elif self.animation_type == "counter_arc":
|
|
self._draw_canvas_counter_arc()
|
|
|
|
def _draw_canvas_line(self):
|
|
center_x, center_y = self.width / 2, self.height / 2
|
|
for i in range(8):
|
|
angle = self.angle + i * (pi / 4)
|
|
start_x = center_x + cos(angle) * (self.width * 0.2)
|
|
start_y = center_y + sin(angle) * (self.height * 0.2)
|
|
end_x = center_x + cos(angle) * (self.width * 0.4)
|
|
end_y = center_y + sin(angle) * (self.height * 0.4)
|
|
alpha = (cos(self.angle * 2 + i * (pi / 4)) + 1) / 2
|
|
|
|
r = int(alpha * (self.highlight_color_rgb[0] - self.color_rgb[0]) + self.color_rgb[0])
|
|
g = int(alpha * (self.highlight_color_rgb[1] - self.color_rgb[1]) + self.color_rgb[1])
|
|
b = int(alpha * (self.highlight_color_rgb[2] - self.color_rgb[2]) + self.color_rgb[2])
|
|
color = f"#{r:02x}{g:02x}{b:02x}"
|
|
|
|
self.create_line(start_x, start_y, end_x, end_y, fill=color, width=2)
|
|
|
|
def _draw_canvas_double_arc(self):
|
|
center_x, center_y = self.width / 2, self.height / 2
|
|
radius = min(center_x, center_y) * 0.8
|
|
bbox = (center_x - radius, center_y - radius, center_x + radius, center_y + radius)
|
|
|
|
start_angle1 = -self.angle * 180 / pi
|
|
extent1 = 120 + 60 * sin(-self.angle)
|
|
self.create_arc(bbox, start=start_angle1, extent=extent1, style=tk.ARC, outline=self.highlight_color, width=2)
|
|
|
|
start_angle2 = (-self.angle + pi) * 180 / pi
|
|
extent2 = 120 + 60 * sin(-self.angle + pi / 2)
|
|
self.create_arc(bbox, start=start_angle2, extent=extent2, style=tk.ARC, outline=self.color, width=2)
|
|
|
|
def _draw_canvas_counter_arc(self):
|
|
center_x, center_y = self.width / 2, self.height / 2
|
|
|
|
radius_outer = min(center_x, center_y) * 0.8
|
|
bbox_outer = (center_x - radius_outer, center_y - radius_outer, center_x + radius_outer, center_y + radius_outer)
|
|
start_angle1 = -self.angle * 180 / pi
|
|
self.create_arc(bbox_outer, start=start_angle1, extent=150, style=tk.ARC, outline=self.highlight_color, width=2)
|
|
|
|
radius_inner = min(center_x, center_y) * 0.6
|
|
bbox_inner = (center_x - radius_inner, center_y - radius_inner, center_x + radius_inner, center_y + radius_inner)
|
|
start_angle2 = self.angle * 180 / pi + 60
|
|
self.create_arc(bbox_inner, start=start_angle2, extent=150, style=tk.ARC, outline=self.color, width=2)
|
|
|
|
def _draw_pillow_frame(self):
|
|
self.draw.rectangle([0, 0, self.width * 4, self.height * 4], fill=(0, 0, 0, 0))
|
|
if self.animation_type == "line":
|
|
self._draw_pillow_line()
|
|
elif self.animation_type == "double_arc":
|
|
self._draw_pillow_double_arc()
|
|
elif self.animation_type == "counter_arc":
|
|
self._draw_pillow_counter_arc()
|
|
|
|
resized_image = self.image.resize((self.width, self.height), Image.Resampling.LANCZOS)
|
|
self.photo_image = ImageTk.PhotoImage(resized_image)
|
|
self.delete("all")
|
|
self.create_image(0, 0, anchor="nw", image=self.photo_image)
|
|
|
|
def _draw_pillow_line(self):
|
|
center_x, center_y = self.width * 2, self.height * 2
|
|
for i in range(12):
|
|
angle = self.angle + i * (pi / 6)
|
|
start_x = center_x + cos(angle) * (self.width * 0.8)
|
|
start_y = center_y + sin(angle) * (self.height * 0.8)
|
|
end_x = center_x + cos(angle) * (self.width * 1.6)
|
|
end_y = center_y + sin(angle) * (self.height * 1.6)
|
|
alpha = (cos(self.angle * 2.5 + i * (pi / 6)) + 1) / 2
|
|
|
|
r = int(alpha * (self.highlight_color_rgb[0] - self.color_rgb[0]) + self.color_rgb[0])
|
|
g = int(alpha * (self.highlight_color_rgb[1] - self.color_rgb[1]) + self.color_rgb[1])
|
|
b = int(alpha * (self.highlight_color_rgb[2] - self.color_rgb[2]) + self.color_rgb[2])
|
|
color = (r, g, b)
|
|
|
|
self.draw.line([(start_x, start_y), (end_x, end_y)], fill=color, width=6, joint="curve")
|
|
|
|
def _draw_pillow_double_arc(self):
|
|
center_x, center_y = self.width * 2, self.height * 2
|
|
radius = min(center_x, center_y) * 0.8
|
|
bbox = (center_x - radius, center_y - radius, center_x + radius, center_y + radius)
|
|
|
|
start_angle1 = -self.angle * 180 / pi
|
|
extent1 = 120 + 60 * sin(-self.angle)
|
|
self.draw.arc(bbox, start=start_angle1, end=start_angle1 + extent1, fill=self.highlight_color_rgb, width=5)
|
|
|
|
start_angle2 = (-self.angle + pi) * 180 / pi
|
|
extent2 = 120 + 60 * sin(-self.angle + pi / 2)
|
|
self.draw.arc(bbox, start=start_angle2, end=start_angle2 + extent2, fill=self.color_rgb, width=5)
|
|
|
|
def _draw_pillow_counter_arc(self):
|
|
center_x, center_y = self.width * 2, self.height * 2
|
|
|
|
radius_outer = min(center_x, center_y) * 0.8
|
|
bbox_outer = (center_x - radius_outer, center_y - radius_outer, center_x + radius_outer, center_y + radius_outer)
|
|
start_angle1 = self.angle * 180 / pi
|
|
self.draw.arc(bbox_outer, start=start_angle1, end=start_angle1 + 150, fill=self.highlight_color_rgb, width=5)
|
|
|
|
radius_inner = min(center_x, center_y) * 0.6
|
|
bbox_inner = (center_x - radius_inner, center_y - radius_inner, center_x + radius_inner, center_y + radius_inner)
|
|
start_angle2 = -self.angle * 180 / pi + 60
|
|
self.draw.arc(bbox_inner, start=start_angle2, end=start_angle2 + 150, fill=self.color_rgb, width=5)
|
|
|
|
def _animate(self):
|
|
if self.running:
|
|
self.angle += 0.1
|
|
if self.angle > 2 * pi:
|
|
self.angle -= 2 * pi
|
|
self._draw_frame()
|
|
self.after(30, self._animate)
|
|
|
|
def start(self):
|
|
if not self.running:
|
|
self.running = True
|
|
self._animate()
|
|
|
|
def stop(self):
|
|
self.running = False
|
|
self.delete("all")
|