Untitled

 avatar
unknown
plain_text
5 months ago
4.4 kB
3
Indexable
class LoadingScreen(ctk.CTkToplevel):
    def __init__(self, parent, message="", dark_mode=False, language='EN'):
        super().__init__(parent)
        
        # Basic setup
        self._destroyed = False
        self.dark_mode = dark_mode
        
        # Configure window
        self.overrideredirect(True)
        self.geometry(f"{self.winfo_screenwidth()}x{self.winfo_screenheight()}+0+0")
        self.attributes('-topmost', True)
        
        # Colors
        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)
        
        # Simple centered layout
        self.frame = ctk.CTkFrame(
            self,
            fg_color=self.bg_color,
            width=400,
            height=400
        )
        self.frame.place(relx=0.5, rely=0.5, anchor="center")
        
        # Fixed-size gif area
        self.gif_frame = ctk.CTkFrame(
            self.frame,
            fg_color="transparent",
            width=200,
            height=200
        )
        self.gif_frame.place(relx=0.5, rely=0.35, anchor="center")
        self.gif_frame.pack_propagate(False)
        
        # GIF label
        self.gif_label = ctk.CTkLabel(
            self.gif_frame,
            text=""
        )
        self.gif_label.place(relx=0.5, rely=0.5, anchor="center")
        
        # Fixed-size message area
        self.message_frame = ctk.CTkFrame(
            self.frame,
            fg_color="transparent",
            width=400,
            height=100
        )
        self.message_frame.place(relx=0.5, rely=0.7, anchor="center")
        self.message_frame.pack_propagate(False)
        
        # Message label
        self.message_label = ctk.CTkLabel(
            self.message_frame,
            text=message,
            font=("Dongle", 32),
            text_color=self.text_color
        )
        self.message_label.place(relx=0.5, rely=0.5, anchor="center")
        
        # Load and start animation
        self._load_animation()
        
    def _load_animation(self):
        """Load animation frames"""
        try:
            suffix = "-w" if self.dark_mode else "-b"
            gif_path = f"icons/loading{suffix}.gif"
            
            self.gif = Image.open(gif_path)
            self.frames = []
            
            # Load all frames
            while True:
                # Convert frame
                frame = self.gif.convert('RGBA')
                frame = frame.resize((200, 200), Image.Resampling.LANCZOS)
                ctk_frame = ctk.CTkImage(
                    light_image=frame,
                    dark_image=frame,
                    size=(200, 200)
                )
                self.frames.append(ctk_frame)
                
                # Move to next frame
                try:
                    self.gif.seek(self.gif.tell() + 1)
                except EOFError:
                    break
            
            # Start animation
            self.current_frame = 0
            self._animate()
            
        except Exception as e:
            print(f"Animation error: {e}")
    
    def _animate(self):
        """Update animation frame"""
        if not self._destroyed and hasattr(self, 'frames') and self.frames:
            # Update frame
            self.gif_label.configure(image=self.frames[self.current_frame])
            self.current_frame = (self.current_frame + 1) % len(self.frames)
            
            # Schedule next frame
            self.after(50, self._animate)
    
    def update_message(self, new_message):
        """Update message text"""
        if not self._destroyed:
            self.message_label.configure(text=new_message)
            self.update_idletasks()
    
    def close(self):
        """Close the loading screen"""
        self._destroyed = True
        self.destroy()

def show_loading_screen(parent, message="", duration=None, dark_mode=False, language='EN'):
    """Helper function to show loading screen"""
    loading_screen = LoadingScreen(
        parent,
        message=message,
        dark_mode=dark_mode,
        language=language
    )
    
    if duration:
        parent.after(int(duration * 1000), loading_screen.close)
    
    return loading_screen
Editor is loading...
Leave a Comment