Untitled

 avatar
unknown
plain_text
5 months ago
8.2 kB
4
Indexable
class WifiConfigurationScreen(ctk.CTkToplevel):
    def __init__(self, parent, dark_mode: bool, language='EN'):
        super().__init__(parent)
        
        self._is_destroyed = False
        self.bind("<Destroy>", self._on_destroy)
        
        # 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 background and theme colors based on dark mode
        self.dark_mode = dark_mode
        bg_color = '#1c1c1e' if dark_mode else '#f5f5f7'
        text_color = 'white' if dark_mode else 'black'
        button_color = '#2c2c2e' if dark_mode else '#e5e5e7'
        button_hover = '#3a3a3c' if dark_mode else '#d1d1d6'
        
        self.configure(fg_color=bg_color)
        
        # Variables initialization
        self.networks = []
        self.selected_network = StringVar()
        self.password = StringVar()
        self.show_password = ctk.BooleanVar(value=False)
        self.keyboard_visible = False
        self.current_connection = {
            "SSID": "Not connected",
            "Signal Strength": "N/A",
            "IP Address": "N/A"
        }
        self.status_message = StringVar(value="Loading... Please wait")
        
        # Load icons for password visibility toggle with correct theme
        icon_suffix = "-w" if dark_mode else "-b"
        self.show_icon = ctk.CTkImage(
            light_image=Image.open(f"icons/eye{icon_suffix}.png"),
            dark_image=Image.open(f"icons/eye{icon_suffix}.png"),
            size=(20, 20)
        )
        self.hide_icon = ctk.CTkImage(
            light_image=Image.open(f"icons/eye-off{icon_suffix}.png"),
            dark_image=Image.open(f"icons/eye-off{icon_suffix}.png"),
            size=(20, 20)
        )

        # Create base container
        self.base_container = ctk.CTkFrame(self, fg_color=bg_color, border_width=0)
        self.base_container.pack(fill="both", expand=True)
        
        # Create main container
        self.main_container = ctk.CTkFrame(self.base_container, fg_color=bg_color, border_width=0)
        self.main_container.pack(fill="both", expand=True)
        
        # Create UI Elements with theme-aware colors
        self.create_widgets(text_color, button_color, button_hover)
        self.scan_wifi_networks()
        self.update_current_connection()

        # Create close button with theme-aware colors
        self.close_button = ctk.CTkButton(
            self,
            text="×",
            width=50,
            height=50,
            command=self.destroy,
            fg_color='#ff3b30',
            hover_color='#ff453a',
            font=("Arial", 24, "bold")
        )
        self.close_button.place(relx=0.98, rely=0.02, anchor="ne")
        self.close_button.lift()
        
        # Bind click outside password entry to hide keyboard
        self.bind('<Button-1>', self.check_hide_keyboard)

    def create_widgets(self, text_color, button_color, button_hover):
        # Top section
        self.top_frame = ctk.CTkFrame(self.main_container, fg_color="transparent")
        self.top_frame.pack(fill="x", padx=20, pady=(40, 0))

        # Title and Status with theme-aware colors
        title_label = ctk.CTkLabel(
            self.top_frame, 
            text="Wi-Fi Configurator",
            font=("Arial", 28, "bold"),
            text_color=text_color
        )
        title_label.pack(pady=(5, 0))

        self.status_label = ctk.CTkLabel(
            self.top_frame,
            textvariable=self.status_message,
            font=("Arial", 14),
            text_color=text_color
        )
        self.status_label.pack(pady=(0, 5))

        # Content section
        self.content_frame = ctk.CTkFrame(self.main_container, fg_color="transparent")
        self.content_frame.pack(fill="both", expand=True, padx=20, pady=(0, 100))

        # Network selection
        network_label = ctk.CTkLabel(
            self.content_frame,
            text="Select Network:",
            font=("Arial", 14),
            text_color=text_color
        )
        network_label.pack(pady=(10, 0))

        self.network_dropdown = ctk.CTkOptionMenu(
            self.content_frame,
            variable=self.selected_network,
            values=self.networks,
            width=250,
            height=35,
            fg_color=button_color,
            button_color=button_hover,
            button_hover_color=button_hover,
            text_color=text_color
        )
        self.network_dropdown.pack(pady=(0, 5))

        # Password entry
        password_label = ctk.CTkLabel(
            self.content_frame,
            text="Password:",
            font=("Arial", 14),
            text_color=text_color
        )
        password_label.pack(pady=(5, 0))

        # Password container
        password_container = ctk.CTkFrame(self.content_frame, fg_color="transparent")
        password_container.pack(pady=(0, 5))

        self.password_entry = ctk.CTkEntry(
            password_container,
            textvariable=self.password,
            show="*",
            width=250,
            height=35,
            fg_color=button_color,
            text_color=text_color
        )
        self.password_entry.pack(side="left", padx=(0, 5))

        self.toggle_password_btn = ctk.CTkButton(
            password_container,
            text="",
            image=self.show_icon,
            width=35,
            height=35,
            fg_color=button_color,
            hover_color=button_hover,
            command=self.toggle_password_visibility
        )
        self.toggle_password_btn.pack(side="left")

        # Action buttons
        button_frame = ctk.CTkFrame(self.content_frame, fg_color="transparent")
        button_frame.pack(pady=(5, 10))

        ctk.CTkButton(
            button_frame,
            text="Connect",
            command=self.start_connecting_to_wifi,
            width=100,
            height=35,
            fg_color=button_color,
            hover_color=button_hover,
            text_color=text_color
        ).grid(row=0, column=0, padx=5)

        ctk.CTkButton(
            button_frame,
            text="Refresh",
            command=self.scan_wifi_networks,
            width=100,
            height=35,
            fg_color=button_color,
            hover_color=button_hover,
            text_color=text_color
        ).grid(row=0, column=1, padx=5)

        # Connection info
        self.connection_frame = ctk.CTkFrame(self.content_frame, fg_color="transparent")
        self.connection_frame.pack(fill="x", pady=(0, 10))

        self.ssid_label = ctk.CTkLabel(
            self.connection_frame,
            text="SSID: Not connected",
            font=("Arial", 12),
            text_color=text_color
        )
        self.ssid_label.pack(pady=1)

        self.signal_label = ctk.CTkLabel(
            self.connection_frame,
            text="Signal Strength: N/A",
            font=("Arial", 12),
            text_color=text_color
        )
        self.signal_label.pack(pady=1)

        self.ip_label = ctk.CTkLabel(
            self.connection_frame,
            text="IP Address: N/A",
            font=("Arial", 12),
            text_color=text_color
        )
        self.ip_label.pack(pady=1)

        # Keyboard container
        self.keyboard_frame = ctk.CTkFrame(self.main_container, fg_color="transparent")
        self.keyboard = TouchKeyboard(
            self.keyboard_frame,
            self.password_entry,
            self.hide_keyboard,
            fg_color="transparent"
        )
        self.keyboard.pack(pady=(0, 20))
        
        # Bind keyboard events
        self.password_entry.bind("<Button-1>", self.show_keyboard)
        self.password_entry.bind("<FocusIn>", self.show_keyboard)

    # [Rest of the methods remain the same]
Editor is loading...
Leave a Comment