Untitled

 avatar
unknown
plain_text
5 months ago
14 kB
3
Indexable
class WiFiDiagnosticsScreen(ctk.CTkFrame):
    def __init__(self, parent, dark_mode: bool, language: str):
        super().__init__(parent)
        
        self.diagnostics = WiFiDiagnostics()
        self.dark_mode = dark_mode
        self.language = language
        
        # Make it fullscreen
        self.place(x=0, y=0, relwidth=1, relheight=1)
        
        # Set background color based on dark mode
        bg_color = '#1c1c1e' if dark_mode else '#f5f5f7'
        text_color = 'white' if dark_mode else 'black'
        self.configure(fg_color=bg_color)
        
        # Create main container with padding
        self.container = ctk.CTkFrame(
            self, 
            fg_color=bg_color,
            corner_radius=0
        )
        self.container.pack(fill='both', expand=True, padx=30, pady=30)
        
        # Create main content box
        self.content_box = ctk.CTkFrame(
            self.container,
            fg_color=bg_color,
            corner_radius=0
        )
        self.content_box.pack(fill='both', expand=True)
        
        # Create the diagnostic content
        self.create_content()
        
        # Start monitoring
        self.diagnostics.start_monitoring(self.update_status)
        
        # Create close button
        try:
            is_light_bg = not dark_mode
            icon_suffix = "-b" if is_light_bg else "-w"
            
            icon_image = ctk.CTkImage(
                light_image=Image.open(f"icons/circle-x{icon_suffix}.png"),
                dark_image=Image.open(f"icons/circle-x{icon_suffix}.png"),
                size=(30, 30)
            )
            self.close_button = ctk.CTkButton(
                self.content_box,
                image=icon_image,
                text="",
                width=30,
                height=30,
                fg_color="transparent",
                hover_color='#2c2c2e' if dark_mode else '#e5e5e7',
                command=self.close_diagnostics
            )
        except Exception as e:
            print(f"Error loading close icon: {e}")
            self.close_button = ctk.CTkButton(
                self.content_box,
                text="✕",
                width=30,
                height=30,
                fg_color="transparent",
                hover_color='#2c2c2e' if dark_mode else '#e5e5e7',
                command=self.close_diagnostics
            )
        
        self.close_button.place(relx=0.95, rely=0.05, anchor='center')

    def create_content(self):
        # Title
        title = ctk.CTkLabel(
            self.content_box,
            text=TRANSLATIONS[self.language]['wifi_diagnostics'],
            font=("Dongle", 36, "bold"),
            text_color='white' if self.dark_mode else 'black'
        )
        title.pack(pady=(20, 40))
        
        # Create two columns
        columns_frame = ctk.CTkFrame(
            self.content_box,
            fg_color='transparent'
        )
        columns_frame.pack(fill='both', expand=True, padx=50)
        
        # Left column - Current Status
        left_column = ctk.CTkFrame(
            columns_frame,
            fg_color='transparent'
        )
        left_column.pack(side='left', fill='both', expand=True, padx=(0, 20))
        
        # Status heading
        status_title = ctk.CTkLabel(
            left_column,
            text=TRANSLATIONS[self.language]['current_network'],
            font=("Dongle", 28, "bold"),
            text_color='white' if self.dark_mode else 'black'
        )
        status_title.pack(anchor='w', pady=(0, 20))
        
        # Status Container
        self.status_container = ctk.CTkFrame(
            left_column,
            fg_color='#2c2c2e' if self.dark_mode else '#e5e5e7',
            corner_radius=15
        )
        self.status_container.pack(fill='x', pady=(0, 20))
        
        # Network status with icon
        network_frame = ctk.CTkFrame(
            self.status_container,
            fg_color='transparent'
        )
        network_frame.pack(fill='x', padx=20, pady=15)
        
        try:
            network_icon = ctk.CTkImage(
                light_image=Image.open(f"icons/wifi{'b' if not self.dark_mode else 'w'}.png"),
                dark_image=Image.open(f"icons/wifi{'b' if not self.dark_mode else 'w'}.png"),
                size=(24, 24)
            )
            network_icon_label = ctk.CTkLabel(
                network_frame,
                image=network_icon,
                text="",
                width=24
            )
            network_icon_label.pack(side='left', padx=(0, 10))
        except Exception:
            pass
            
        self.network_label = ctk.CTkLabel(
            network_frame,
            text="-",
            font=("Dongle", 24),
            text_color='white' if self.dark_mode else 'black'
        )
        self.network_label.pack(side='left', fill='x', expand=True)
        
        # Connection status
        self.connection_label = ctk.CTkLabel(
            self.status_container,
            text=TRANSLATIONS[self.language]['wifi_disconnected'],
            font=("Dongle", 24),
            text_color='#ff453a'
        )
        self.connection_label.pack(padx=20, pady=(0, 15))
        
        # Signal strength with icon
        signal_frame = ctk.CTkFrame(
            self.status_container,
            fg_color='transparent'
        )
        signal_frame.pack(fill='x', padx=20, pady=(0, 15))
        
        try:
            signal_icon = ctk.CTkImage(
                light_image=Image.open(f"icons/signal{'b' if not self.dark_mode else 'w'}.png"),
                dark_image=Image.open(f"icons/signal{'b' if not self.dark_mode else 'w'}.png"),
                size=(24, 24)
            )
            signal_icon_label = ctk.CTkLabel(
                signal_frame,
                image=signal_icon,
                text="",
                width=24
            )
            signal_icon_label.pack(side='left', padx=(0, 10))
        except Exception:
            pass
            
        self.signal_label = ctk.CTkLabel(
            signal_frame,
            text="Signal Strength: -",
            font=("Dongle", 24),
            text_color='white' if self.dark_mode else 'black'
        )
        self.signal_label.pack(side='left', fill='x', expand=True)
        
        # IP Address with icon
        ip_frame = ctk.CTkFrame(
            self.status_container,
            fg_color='transparent'
        )
        ip_frame.pack(fill='x', padx=20, pady=(0, 15))
        
        try:
            ip_icon = ctk.CTkImage(
                light_image=Image.open(f"icons/ip{'b' if not self.dark_mode else 'w'}.png"),
                dark_image=Image.open(f"icons/ip{'b' if not self.dark_mode else 'w'}.png"),
                size=(24, 24)
            )
            ip_icon_label = ctk.CTkLabel(
                ip_frame,
                image=ip_icon,
                text="",
                width=24
            )
            ip_icon_label.pack(side='left', padx=(0, 10))
        except Exception:
            pass
            
        self.ip_label = ctk.CTkLabel(
            ip_frame,
            text="IP: -",
            font=("Dongle", 24),
            text_color='white' if self.dark_mode else 'black'
        )
        self.ip_label.pack(side='left', fill='x', expand=True)
        
        # Right column - Available Networks
        right_column = ctk.CTkFrame(
            columns_frame,
            fg_color='transparent'
        )
        right_column.pack(side='left', fill='both', expand=True, padx=(20, 0))
        
        # Available Networks heading
        networks_title = ctk.CTkLabel(
            right_column,
            text="Available Networks",
            font=("Dongle", 28, "bold"),
            text_color='white' if self.dark_mode else 'black'
        )
        networks_title.pack(anchor='w', pady=(0, 20))
        
        # Networks list
        self.networks_frame = ctk.CTkScrollableFrame(
            right_column,
            fg_color='#2c2c2e' if self.dark_mode else '#e5e5e7',
            corner_radius=15,
            height=250
        )
        self.networks_frame.pack(fill='x', pady=(0, 20))
        
        # Network password entry field (hidden by default)
        self.password_frame = ctk.CTkFrame(
            right_column,
            fg_color='transparent'
        )
        
        self.password_entry = ctk.CTkEntry(
            self.password_frame,
            placeholder_text="Enter network password",
            font=("Dongle", 20),
            height=40,
            fg_color='#2c2c2e' if self.dark_mode else '#e5e5e7',
            text_color='white' if self.dark_mode else 'black',
            placeholder_text_color='#86868b'
        )
        self.password_entry.pack(side='left', fill='x', expand=True, padx=(0, 10))
        
        self.connect_button = ctk.CTkButton(
            self.password_frame,
            text="Connect",
            font=("Dongle", 20),
            height=40,
            fg_color='#34c759' if not self.dark_mode else '#30d158',
            hover_color='#32b357',
            command=self.connect_to_network
        )
        self.connect_button.pack(side='right')
        
        # Refresh button at the bottom
        self.refresh_button = ctk.CTkButton(
            right_column,
            text=TRANSLATIONS[self.language]['reconnect'],
            command=self.refresh_networks,
            font=("Dongle", 24),
            height=40,
            fg_color='#2c2c2e' if self.dark_mode else '#e5e5e7',
            hover_color='#3a3a3c' if self.dark_mode else '#d1d1d6',
            text_color='white' if self.dark_mode else 'black'
        )
        self.refresh_button.pack(fill='x')
        
        # Initial network scan
        self.refresh_networks()

    def select_network(self, network_name):
        self.selected_network = network_name
        self.password_frame.pack(fill='x', pady=(0, 20))
        self.password_entry.delete(0, 'end')
        self.password_entry.focus()
        
        # Update network buttons
        for widget in self.networks_frame.winfo_children():
            if isinstance(widget, ctk.CTkButton):
                if widget.cget("text") == network_name:
                    widget.configure(
                        fg_color='#34c759' if not self.dark_mode else '#30d158'
                    )
                else:
                    widget.configure(
                        fg_color='#3a3a3c' if self.dark_mode else '#d1d1d6'
                    )

    def connect_to_network(self):
        if hasattr(self, 'selected_network'):
            password = self.password_entry.get()
            print(f"Connecting to {self.selected_network} with password {password}")
            # Here you would add the actual WiFi connection code
            # For example, using wpa_cli or similar
            
            # Hide password frame
            self.password_frame.pack_forget()
            self.password_entry.delete(0, 'end')

    def update_status(self, status: Dict):
        if not hasattr(self, 'network_label'):
            return
            
        # Update network name
        self.network_label.configure(
            text=status['network'] or "-"
        )
        
        # Update signal strength
        signal_str = "-"
        if status['signal'] is not None:
            strength = abs(status['signal'])
            if strength < 50:
                signal_str = "Excellent"
            elif strength < 60:
                signal_str = "Good"
            elif strength < 70:
                signal_str = "Fair"
            else:
                signal_str = "Poor"
            signal_str = f"{signal_str} ({status['signal']} dBm)"
        self.signal_label.configure(text=f"Signal Strength: {signal_str}")
        
        # Update IP address
        self.ip_label.configure(text=f"IP: {status['ip'] or '-'}")
        
        # Update connection status
        if status['internet']:
            self.connection_label.configure(
                text=TRANSLATIONS[self.language]['wifi_connected'],
                text_color='#34c759' if not self.dark_mode else '#30d158'
            )
        else:
            self.connection_label.configure(
                text=TRANSLATIONS[self.language]['wifi_disconnected'],
                text_color='#ff453a'
            )

    def refresh_networks(self):
        # Clear existing network buttons
        for widget in self.networks_frame.winfo_children():
            widget.destroy()
        
        # Show loading indicator
        loading_label = ctk.CTkLabel(
            self.networks_frame,
            text=TRANSLATIONS[self.language]['diagnostic_running'],
            font=("Dongle", 24),
            text_color='white' if self.dark_mode else 'black'
        )
        loading_label.pack(pady=10)
        
        def update_networks():
            networks = self.diagnostics.get_available_networks()
            
            # Remove loading label
            loading_label.destroy()
            
            # Add network buttons
            for network in networks:
                network_button = ctk.CTkButton(
                    self.networks_frame,
                    text=network['ssid'],
                    font=("Dongle", 20),
                    height=40,
                    fg_color='#3a3a3c' if self.dark_mode else '#d1d1d6',
                    hover_color='#34c759' if not self.dark_mode else '#30d158',
                    text_color='white' if self.dark_mode else 'black',
                    command=lambda n=network['ssid']: self.select_network(n)
                )
                network_button.pack(fill='x', pady=5, padx=10
Editor is loading...
Leave a Comment