Untitled

 avatar
unknown
plain_text
5 months ago
4.6 kB
2
Indexable
class LoadingScreen(ctk.CTkToplevel):
    def __init__(self, parent, message="", dark_mode=False, language='EN'):
        super().__init__(parent)
        
        # 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)

        # Set colors based on dark mode
        self.bg_color = '#1c1c1e' if dark_mode else '#f5f5f7'
        self.text_color = 'white' if dark_mode else 'black'
        self.configure(fg_color=self.bg_color)
        
        # Create main container
        self.container = ctk.CTkFrame(
            self,
            fg_color=self.bg_color,
            corner_radius=0
        )
        self.container.pack(fill="both", expand=True)
        
        # Create content frame for vertical centering
        self.content_frame = ctk.CTkFrame(
            self.container,
            fg_color="transparent"
        )
        self.content_frame.place(relx=0.5, rely=0.5, anchor="center")
        
        # Create label for GIF
        self.loading_label = ctk.CTkLabel(
            self.content_frame,
            text="",
        )
        self.loading_label.pack(pady=(0, 20))
        
        # Load and start GIF animation
        self.load_gif(dark_mode)
        
        # Create frame for messages
        self.messages_frame = ctk.CTkFrame(
            self.content_frame,
            fg_color="transparent"
        )
        self.messages_frame.pack()
        
        # List to keep track of message labels
        self.message_labels = []
        
        # Add initial message if provided
        if message:
            self.add_message(message)

    def load_gif(self, dark_mode):
        """Load and prepare GIF frames"""
        try:
            # Determine which GIF to use based on dark mode
            suffix = "-w" if dark_mode else "-b"
            gif_path = f"icons/loading{suffix}.gif"
            
            # Open the GIF file
            self.gif = Image.open(gif_path)
            
            # Store the frames
            self.frames = []
            try:
                while True:
                    # Convert frame to RGBA if it isn't already
                    frame = self.gif.convert('RGBA')
                    
                    # Resize with high-quality resampling
                    frame = frame.resize(
                        (200, 200),
                        Image.Resampling.LANCZOS
                    )
                    
                    self.frames.append(frame)
                    self.gif.seek(self.gif.tell() + 1)
            except EOFError:
                pass
            
            # Start animation
            self.current_frame = 0
            self.animate_gif()
            
        except Exception as e:
            print(f"Error loading gif: {e}")
    
    def animate_gif(self):
        """Display the next frame of the GIF"""
        if hasattr(self, 'frames') and self.frames and not hasattr(self, '_destroyed'):
            # Create CTkImage for current frame
            current_frame = self.frames[self.current_frame]
            ctk_image = ctk.CTkImage(
                light_image=current_frame,
                dark_image=current_frame,
                size=(200, 200)
            )
            
            # Update the label with the new frame
            self.loading_label.configure(image=ctk_image)
            
            # Move to next frame
            self.current_frame = (self.current_frame + 1) % len(self.frames)
            
            # Schedule next frame update
            duration = self.gif.info.get('duration', 50)
            self.after(duration, self.animate_gif)

    def add_message(self, message):
        """Add a new message below existing ones"""
        label = ctk.CTkLabel(
            self.messages_frame,
            text=message,
            font=("Dongle", 32),
            text_color=self.text_color
        )
        label.pack(pady=5)
        self.message_labels.append(label)

    def update_message(self, new_message):
        """Add a new message instead of replacing the old one"""
        self.add_message(new_message)

    def close(self):
        """Safely close the loading screen"""
        self._destroyed = True  # Stop animation
        self.destroy()
Editor is loading...
Leave a Comment