Untitled

 avatar
unknown
plain_text
5 months ago
6.1 kB
4
Indexable
class ClassificationResultScreen(ctk.CTkToplevel):
    def __init__(self, parent, waste_type: str, dark_mode: bool, language='EN'):
        super().__init__(parent)
        
        # Constants for animation
        self.DISPLAY_TIME = 5  # seconds
        self.PROGRESS_HEIGHT = 6
        self.start_time = time.time()
        self._destroyed = False
        
        # Store parameters
        self.waste_type = waste_type
        self.dark_mode = dark_mode
        self.language = language
        
        # Get bin configuration for proper color mapping
        bin_config = BinConfiguration()
        self.current_bin = None
        
        # Find the bin configuration by name
        for bin_item in bin_config.bins:
            if bin_item['name'] == waste_type:
                self.current_bin = bin_item
                break
        
        # Get the appropriate color based on bin configuration and dark mode
        if self.current_bin:
            self.bg_color = self.current_bin['color_dark'] if dark_mode else self.current_bin['color']
        else:
            # Fallback to default colors if bin not found
            color_mapping = {
                'Paper': ('#0d7dd4', '#e6f3ff'),
                'Papier': ('#0d7dd4', '#e6f3ff'),
                'Recycling': ('#fec20c', '#fff3d6'),
                'Gelber Sack': ('#fec20c', '#fff3d6'),
                'Organic': ('#8a6849', '#e8d5c4'),
                'Biomüll': ('#8a6849', '#e8d5c4'),
                'Residual': ('#3a3a3c', '#cececf'),
                'Restmüll': ('#3a3a3c', '#cececf')
            }
            colors = color_mapping.get(waste_type, ('#3a3a3c', '#cececf'))
            self.bg_color = colors[0] if dark_mode else colors[1]
        
        # Make it fullscreen and disable all close operations
        self.overrideredirect(True)
        self.geometry(f"{self.winfo_screenwidth()}x{self.winfo_screenheight()}+0+0")
        self.protocol("WM_DELETE_WINDOW", lambda: None)
        self.attributes('-topmost', True)
        self.configure(fg_color=self.bg_color)
        
        # Determine text color based on background brightness
        hex_color = self.bg_color.lstrip('#')
        rgb = tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4))
        brightness = (rgb[0] * 299 + rgb[1] * 587 + rgb[2] * 114) / 1000
        self.text_color = '#000000' if brightness > 128 else '#ffffff'
        
        # Create progress bar frame at the top
        self.progress_frame = ctk.CTkFrame(
            self,
            height=self.PROGRESS_HEIGHT,
            fg_color=self.text_color,
            corner_radius=0
        )
        self.progress_frame.pack(fill='x', pady=0)
        
        # Create progress bar
        self.progress_bar = ctk.CTkFrame(
            self.progress_frame,
            height=self.PROGRESS_HEIGHT,
            fg_color=self.bg_color,
            corner_radius=0
        )
        self.progress_bar.place(relx=0, rely=0, relwidth=1)
        
        # Create main container
        self.container = ctk.CTkFrame(
            self,
            fg_color=self.bg_color,
            corner_radius=0
        )
        self.container.pack(fill='both', expand=True)
        
        # Add the result text
        self.result_label = ctk.CTkLabel(
            self.container,
            text=self.waste_type,
            font=('Dongle', 48, 'bold'),
            text_color=self.text_color
        )
        self.result_label.pack(pady=(80, 20))

        self.bind("<Destroy>", self._on_destroy)
        
        # Add icon
        try:
            # Determine icon name based on bin configuration or default mapping
            icon_name = None
            if self.current_bin:
                # Extract icon name from bin ID (e.g., "bin1" -> "organic")
                bin_type_mapping = {
                    'bin1': 'organic',
                    'bin2': 'recycling',
                    'bin3': 'paper',
                    'bin4': 'residual'
                }
                icon_name = bin_type_mapping.get(self.current_bin['id'])
            else:
                # Use default icon mapping
                icon_mapping = {
                    'Paper': 'paper',
                    'Papier': 'paper',
                    'Recycling': 'recycling',
                    'Gelber Sack': 'recycling',
                    'Organic': 'organic',
                    'Biomüll': 'organic',
                    'Residual': 'residual',
                    'Restmüll': 'residual'
                }
                icon_name = icon_mapping.get(waste_type)
            
            if icon_name:
                icon_path = f"icons/{icon_name}.png"
                icon_image = ctk.CTkImage(
                    light_image=Image.open(icon_path),
                    dark_image=Image.open(icon_path),
                    size=(120, 120)
                )
                self.icon_label = ctk.CTkLabel(
                    self.container,
                    text="",
                    image=icon_image
                )
                self.icon_label.pack(pady=20)
        except Exception as e:
            print(f"Error loading icon: {e}")
        
        # Start countdown
        self.update_progress()

    def _on_destroy(self, event):
        """Handle the window destruction event"""
        if event.widget is self:
            self._destroyed = True

    def update_progress(self):
        """Update the progress bar and check if window should close"""
        if self._destroyed:
            return
            
        elapsed = time.time() - self.start_time
        remaining = max(0, 1 - (elapsed / self.DISPLAY_TIME))
        
        if remaining > 0:
            self.progress_bar.place_configure(relwidth=remaining)
            self.after(16, self.update_progress)  # Update roughly every frame
        else:
            # Only destroy if not already destroyed
            if not self._destroyed:
                self.destroy()
Editor is loading...
Leave a Comment