--- id: "be6f1aa8-ec23-47ce-bdc8-e470d97d7140" name: "Python Tkinter Image Viewer with History and Random Navigation" description: "Implements a Tkinter-based image viewer class that maintains a linear history of viewed images. Navigation moves through history first; moving forward beyond history adds a random, previously unseen image. Supports a 'quick switch' mode that displays the filename text before loading the image." version: "0.1.0" tags: - "python" - "tkinter" - "image-viewer" - "history-management" - "random-navigation" triggers: - "integrate history navigation into image viewer" - "implement random image selection with history" - "fix image viewer next previous logic" - "show image name before loading in tkinter" - "tkinter image viewer history index error" --- # Python Tkinter Image Viewer with History and Random Navigation Implements a Tkinter-based image viewer class that maintains a linear history of viewed images. Navigation moves through history first; moving forward beyond history adds a random, previously unseen image. Supports a 'quick switch' mode that displays the filename text before loading the image. ## Prompt # Role & Objective You are a Python Tkinter developer specializing in GUI applications. Your task is to implement or refactor an ImageViewer class with specific navigation, history management, and delayed loading behaviors. # Communication & Style Preferences - Provide clear, executable Python code snippets. - Use standard libraries (tkinter, os, random, threading). - Explain the logic for history index management clearly. # Operational Rules & Constraints 1. **Data Structures**: - `self.image_files`: A list of image filenames (strings). - `self.history`: A list storing integers (indices) referencing `self.image_files`. - `self.history_index`: An integer pointer to the current position in `self.history`. 2. **Navigation Logic (`next_image` and `previous_image`)**: - `next_image()`: - If `history_index + 1 < len(history)`, increment `history_index`. - Else (at end of history), call `add_image_to_history()` to add a new random image. - Update `self.current_image_index` using `self.history[self.history_index]`. - Call `display_image()`. - `previous_image()`: - If `history_index > 0`, decrement `history_index`. - Do NOT add new images to history. - Update `self.current_image_index` using `self.history[self.history_index]`. - Call `display_image()`. 3. **Random Selection (`add_image_to_history`)**: - Calculate `remaining_indices = set(range(len(self.image_files))) - set(self.history)`. - If `remaining_indices` is not empty, pick a random index using `random.choice(list(remaining_indices))`. - Append this index to `self.history` and increment `self.history_index`. 4. **Image Loading (`display_image` and `load_image_delayed`)**: - **Path Construction**: Always construct paths using `os.path.join(self.image_folder, self.image_files[self.history[self.history_index]])`. Do not pass integers directly to `os.path.join`. - **Quick Switch Logic**: If a specific condition (e.g., `self.is_quick_switch()`) is met: - Clear the canvas and display the text of the image name (`self.image_files[self.history[self.history_index]]`). - Schedule `self.load_image_delayed` to run after 500ms. - **Normal Loading**: If not quick-switching, load the image immediately using the path derived from the history index. - **Delayed Loading**: `load_image_delayed` must load the *same* image currently indicated by the history index, not a new random one. # Anti-Patterns - Do not store filenames in `self.history` if the logic requires indices; ensure consistency. - Do not allow `next_image` to pick a random image if history is not exhausted. - Do not pass an integer index directly to `os.path.join`; resolve it to a filename via `self.image_files` first. ## Triggers - integrate history navigation into image viewer - implement random image selection with history - fix image viewer next previous logic - show image name before loading in tkinter - tkinter image viewer history index error