import os import json import tkinter as tk from tkinter import filedialog, messagebox BASE_DIR = os.path.dirname(os.path.abspath(__file__)) CONFIG_FILE = os.path.join(BASE_DIR, "config.json") class DirectorySelectorApp: def __init__(self, root): self.root = root self.root.title("Selector de Directorios") self.root.geometry("600x550") # Variables de rutas self.path_esde_src = tk.StringVar() self.path_roms_src = tk.StringVar() self.path_esde_dst = tk.StringVar() self.path_roms_dst = tk.StringVar() # --------------------------- # RUTAS DE ORIGEN # --------------------------- tk.Label(root, text="Rutas de origen", font=("Arial", 12, "bold")).pack(pady=(10, 0)) self.create_path_selector("Origen ES-DE:", self.path_esde_src) self.create_path_selector("Origen ROMs:", self.path_roms_src, callback=self.update_directory_list) # --------------------------- # LISTA DE DIRECTORIOS # --------------------------- tk.Label(root, text="Directorios encontrados en ROMs:", font=("Arial", 11)).pack(pady=(15, 5)) self.listbox = tk.Listbox(root, selectmode=tk.MULTIPLE, height=15) self.listbox.pack(fill="both", expand=True, padx=10) # --------------------------- # RUTAS DE DESTINO # --------------------------- tk.Label(root, text="Rutas de destino", font=("Arial", 12, "bold")).pack(pady=(15, 0)) self.create_path_selector("Destino ES-DE:", self.path_esde_dst) self.create_path_selector("Destino ROMs:", self.path_roms_dst) # Evento de cierre self.root.protocol("WM_DELETE_WINDOW", self.on_close) # Cargar configuración previa self.load_config() # --------------------------------------------------------- # CREAR SELECTOR DE RUTA # --------------------------------------------------------- def create_path_selector(self, label_text, var, callback=None): frame = tk.Frame(self.root) frame.pack(fill="x", padx=10, pady=5) tk.Label(frame, text=label_text, width=15, anchor="w").pack(side="left") tk.Entry(frame, textvariable=var, width=50).pack(side="left", padx=5) tk.Button(frame, text="Buscar", command=lambda: self.choose_path(var, callback)).pack(side="left") def choose_path(self, var, callback): path = filedialog.askdirectory() if not path: return var.set(path) if callback: callback(path) # --------------------------------------------------------- # LISTA DE DIRECTORIOS # --------------------------------------------------------- def update_directory_list(self, path): self.listbox.delete(0, tk.END) if not os.path.isdir(path): return try: dirs = sorted( d for d in os.listdir(path) if os.path.isdir(os.path.join(path, d)) ) for d in dirs: self.listbox.insert(tk.END, d) except Exception as e: messagebox.showerror("Error", f"No se pudo leer la ruta:\n{e}") # --------------------------------------------------------- # PERSISTENCIA # --------------------------------------------------------- def save_config(self): data = { "esde_src": self.path_esde_src.get(), "roms_src": self.path_roms_src.get(), "esde_dst": self.path_esde_dst.get(), "roms_dst": self.path_roms_dst.get(), "selected": [self.listbox.get(i) for i in self.listbox.curselection()] } try: with open(CONFIG_FILE, "w", encoding="utf-8") as f: json.dump(data, f, indent=4) except Exception as e: messagebox.showerror("Error", f"No se pudo guardar la configuración:\n{e}") def load_config(self): if not os.path.exists(CONFIG_FILE): return try: with open(CONFIG_FILE, "r", encoding="utf-8") as f: data = json.load(f) except Exception: return # Restaurar rutas self.path_esde_src.set(data.get("esde_src", "")) self.path_roms_src.set(data.get("roms_src", "")) self.path_esde_dst.set(data.get("esde_dst", "")) self.path_roms_dst.set(data.get("roms_dst", "")) # Actualizar lista si ROMs origen existe roms_path = self.path_roms_src.get() if os.path.isdir(roms_path): self.update_directory_list(roms_path) # Restaurar selección selected = set(data.get("selected", [])) for i in range(self.listbox.size()): if self.listbox.get(i) in selected: self.listbox.selection_set(i) # --------------------------------------------------------- # CIERRE # --------------------------------------------------------- def on_close(self): self.save_config() self.root.destroy() if __name__ == "__main__": root = tk.Tk() app = DirectorySelectorApp(root) root.mainloop()