import os from tkinter import Tk, Button, Label, filedialog, messagebox, IntVar,…
- User
import os from tkinter import Tk, Button, Label, filedialog, messagebox, IntVar, Checkbutton, Spinbox
Assuming Image importing from PIL is already done (if needed)
class ImageBrowser: def init(self, master): self.master = master self.master.title("Image Browser") self.history_pool = [] # This will be the shuffled list of images
# Initialize variables self.img_EXTENSIONS = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp'] self.file_index = [] self.current_index = 0 self.image_folder = "" self.limit_reached = False # Create widgets self.button_open = Button(master, text="Open Image Folder", command=self.open_image_folder) self.button_open.pack() self.button_previous = Button(master, text="Previous Image", command=self.previous_image) self.button_previous.pack(side='left') self.button_next = Button(master, text="Next Image", command=self.next_image) self.button_next.pack(side='right') self.limit_var = IntVar() self.limit_checkbox = Checkbutton(master, text="Limit", variable=self.limit_var) self.limit_checkbox.pack() self.limit_spinbox = Spinbox(master, from_=1, to=100, width=5) self.limit_spinbox.pack() # Image info labels self.label_total_img = Label(master, text="Total Images: 0") self.label_total_img.pack() self.label_img_count = Label(master, text="Image Count: 0") self.label_img_count.pack() self.label_img_name = Label(master, text="Image Name: -") self.label_img_name.pack() def open_image_folder(self): folder_path = filedialog.askdirectory() if folder_path: self.image_folder = folder_path self.file_index = [ os.path.join(self.image_folder, f) for f in os.listdir(self.image_folder) if os.path.splitext(f)[1].lower() in self.img_EXTENSIONS ] self.file_index.sort() if not self.file_index: messagebox.showinfo("No Images", "No images found in the selected folder.") return self.current_index = 0 self.limit_reached = False self.label_total_img.config(text=f"Total Images: {len(self.file_index)}") self.update_image_info() elif not self.file_index: messagebox.showinfo("No Folder", "Please select a folder to start browsing images.") def next_image(self): if not self.file_index: messagebox.showinfo("No Images", "Please open an image folder first.") return next_count = (self.current_index + 1) % len(self.file_index) if self.limit_var.get(): limit = int(self.limit_spinbox.get()) if self.limit_reached: self.limit_reached = False self.current_index = next_count elif next_count % limit == 0: # Limit is reached self.label_img_name.config(text="End of limit") self.limit_reached = True self.label_img_count.config(text=f"Image Count: {next_count}") return self.current_index = next_count self.update_image_info() def previous_image(self): if not self.file_index: messagebox.showinfo("No Images", "Please open an image folder first.") return self.limit_reached = False # View the previous image regardless of limit self.current_index = (self.current_index - 1) % len(self.file_index) self.update_image_info() def update_image_info(self): current_img_path = self.file_index[self.current_index] img_name = os.path.basename(current_img_path) self.label_img_name.config(text=f"Image Name: {img_name}") self.label_img_count.config(text=f"Image Count: {self.current_index + 1}") # You would add logic here to actually display the image, if desired - Assistant
- User
instead of updating the code give me a new and better code with that behavior. when i press open image, set file index and call an image from the index.put it in the history pool. make the program calls from the history pool. make the next button calls random image number and add it to the pool. when i press previous, make it call from the history pool. and if it reach the top of the pool, it adds a random image to the pool. make the next button call from the pool until it reach the bottom of the pool then add new random image to the pool from the index. keep the limit button showing an end limit text for every (limit number) make it so that the next button and previous button will show every image in the folder once before starting another cycle.
- Assistant
- User
where's the limit checkbutton?
- Assistant
- User
make the limit just show end of limit every (limit number of image in the spinbox) instead of just end at limit. if i press next when "end of limit" is reached, it just call another next random image in the pool so the program doesn't end there
- Assistant
- User
if there's no more images a the top of history pool, make the previous button add a random image that hasn't been called to the top of the history pool
- Assistant
Below is a modification of the ImageBrowser class that implements the behavior you requested. If the previous button is pressed and there are no more images at the top of the history pool, it will add a random image that hasn't been called to the top of the history pool:
- User
no, when the previous button has reached the first image, add a random image as a new first image in the pool instead of showing a message. so that the program doesn't end at the first image
- Assistant
- User
after going back, make the next button call from the pool of history of shown images instead of immediately calling a new random image
- Assistant