Untitled

 avatar
unknown
plain_text
a month ago
5.6 kB
4
Indexable
    def reload_ui(self):
        root = self.winfo_toplevel()
        current_dark_mode = root.DARK_MODE
        current_language = root.LANGUAGE
        
        # Store turntable reference
        old_turntable = CircularProgress._turntable

        # Force language update in all instances
        for instance in CircularProgress._instances:
            if hasattr(instance, 'language'):
                instance.language = current_language

        # Get bin configuration
        bin_config = BinConfiguration()
        
        # Store these values before destroying widgets
        sayings = load_sayings()
        current_language_sayings = sayings[current_language]
        random_saying = random.choice(current_language_sayings)

        # Store reference to existing classification prompt if it exists
        existing_prompt = None
        old_prompt_turntable = None
        for widget in root.winfo_children():
            if isinstance(widget, ClassificationPrompt):
                existing_prompt = widget
                old_prompt_turntable = widget.turntable
                break
        
        # Recreate all UI elements
        for widget in root.winfo_children():
            if not isinstance(widget, ClassificationPrompt):  # Don't destroy ClassificationPrompt
                widget.destroy()
        
        # Recreate main frame and elements
        main_frame = ctk.CTkFrame(root, fg_color='#1c1c1e' if current_dark_mode else '#f5f5f7')
        main_frame.pack(fill='both', expand=True)
        
        # Create saying label first
        saying_label = create_saying_label(main_frame, current_dark_mode)
        saying_label.configure(text=random_saying)
        
        # Recreate settings menu
        settings_menu = SettingsMenu(main_frame, current_dark_mode)

        # Recreate reset button
        try:
            is_light_bg = not current_dark_mode
            icon_suffix = "-b" if is_light_bg else "-w"
            
            reset_icon = ctk.CTkImage(
                light_image=Image.open(f"icons/reset{icon_suffix}.png"),
                dark_image=Image.open(f"icons/reset{icon_suffix}.png"),
                size=(30, 30)
            )
            reset_button = ctk.CTkButton(
                main_frame,
                image=reset_icon,
                text="",
                width=30,
                height=30,
                fg_color="transparent",
                hover_color='#2c2c2e' if current_dark_mode else '#e5e5e7',
                command=lambda: ResetFillLevelsScreen(root, current_dark_mode, current_language)
            )
            reset_button.place(relx=0.03, rely=0.05, anchor='center')
            
        except Exception as e:
            print(f"Error loading reset icon: {e}")
            reset_button = ctk.CTkButton(
                main_frame,
                text="?",
                width=30,
                height=30,
                fg_color="transparent",
                hover_color='#2c2c2e' if current_dark_mode else '#e5e5e7',
                command=lambda: ResetFillLevelsScreen(root, current_dark_mode, current_language)
            )
            reset_button.place(relx=0.03, rely=0.05, anchor='center')
        
        # Recreate close button
        close_button = CloseButton(main_frame, command=root.destroy)
        close_button.place(relx=0.05, rely=0.95, anchor='center')
        
        # Recreate circles frame and elements
        circles_frame = ctk.CTkFrame(main_frame, fg_color='#1c1c1e' if current_dark_mode else '#f5f5f7')
        circles_frame.place(relx=0.5, rely=0.5, anchor='center')
        
        # Add padding frame
        padding_frame = ctk.CTkFrame(circles_frame, fg_color='#1c1c1e' if current_dark_mode else '#f5f5f7')
        padding_frame.pack(padx=50)
        
        # Create circles for each bin from configuration
        for bin_config_item in bin_config.bins:
            container = ctk.CTkFrame(padding_frame, fg_color='#1c1c1e' if current_dark_mode else '#f5f5f7')
            container.pack(side='left', padx=15)
            
            progress = CircularProgress(
                container,
                bin_id=bin_config_item['id'],
                size=220
            )
            progress.pack()
            progress.set_dark_mode(current_dark_mode)

            # Rebind the button functionality
            progress.bind('<Button-1>', progress.on_press)
            progress.bind('<ButtonRelease-1>', progress.on_release)

        # After recreating circles, restore turntable reference
        CircularProgress._turntable = old_turntable

        # Update classification prompt
        if existing_prompt:
            existing_prompt.update_appearance(current_dark_mode, current_language)
            existing_prompt.turntable = old_prompt_turntable        
        
        # Make sure turntable is properly initialized before allowing calibration
        if old_turntable and not old_turntable.is_initialized:
            print("Re-initializing turntable after UI reload")
            try:
                old_turntable.initialize_hardware()
            except Exception as e:
                print(f"Error re-initializing turntable: {e}")

        # Make sure to reset servo states
        for instance in CircularProgress._instances:
            if not instance._destroyed:
                instance.servo_active = False
                instance.press_scale = 1.0
                instance.press_animation_active = False
Leave a Comment