Untitled
def draw(self): self.delete('all') # Get colors from bin configuration circle_color = self.bin_config['color_dark'] if self.dark_mode else self.bin_config['color'] ring_color = self.get_ring_color() progress_color = self.get_progress_color() bg_color = '#1c1c1e' if self.dark_mode else '#f5f5f7' # Apply scale transform for press animation scaled_size = self.size * self.press_scale # Create new image for drawing self.im = Image.new('RGBA', (1000, 1000), bg_color) draw = ImageDraw.Draw(self.im) # Calculate dimensions outer_padding = 40 ring_width = 40 circle_padding = 15 # Draw white background circle in light mode if not self.dark_mode: draw.ellipse((outer_padding-ring_width, outer_padding-ring_width, 1000-outer_padding+ring_width, 1000-outer_padding+ring_width), fill='white') # Draw ring (trough) draw.arc((outer_padding-ring_width, outer_padding-ring_width, 1000-outer_padding+ring_width, 1000-outer_padding+ring_width), -90, 270, ring_color, ring_width) # Draw progress arc if self.fill_level > 0: angle = int(self.fill_level * 360 / 100) draw.arc((outer_padding-ring_width, outer_padding-ring_width, 1000-outer_padding+ring_width, 1000-outer_padding+ring_width), -90, -90 + angle, progress_color, ring_width) # Draw base circle if not self.dark_mode: draw.ellipse((outer_padding + circle_padding - 1, outer_padding + circle_padding - 1, 1000-outer_padding - circle_padding + 1, 1000-outer_padding - circle_padding + 1), fill='white') draw.ellipse((outer_padding + circle_padding, outer_padding + circle_padding, 1000-outer_padding - circle_padding, 1000-outer_padding - circle_padding), fill=circle_color) # Resize and create PhotoImage resized = self.im.resize((int(scaled_size), int(scaled_size)), Image.Resampling.LANCZOS) self.arc = ImageTk.PhotoImage(resized) # Calculate position x = (self.size - scaled_size) / 2 y = (self.size - scaled_size) / 2 # Display the image self.create_image(self.size/2, self.size/2, image=self.arc) # Add text text_color = self.get_text_color(circle_color) self.create_text( self.size/2, self.size/2, text=self.bin_config['name'], font=('Dongle', int(18 * self.press_scale), 'normal'), fill=text_color, justify='center', width=self.size-20 ) def get_text_color(self, background_color): color = background_color.lstrip('#') rgb = tuple(int(color[i:i+2], 16) for i in (0, 2, 4)) luminance = (0.299 * rgb[0] + 0.587 * rgb[1] + 0.114 * rgb[2]) / 255 return '#000000' if luminance > 0.5 else '#ffffff' def on_press(self, event): current_time = time.time() if current_time - self.last_press_time >= self.cooldown_period: self.is_pressed = True self.press_animation_active = True self.last_press_time = current_time self.can_press = False print(f"{self.bin_id} button pressed") # Add your button press handling here def on_release(self, event): self.is_pressed = False
Leave a Comment