This commit is contained in:
2026-01-20 13:53:45 +01:00
parent 08973257a8
commit a867f7dc85
2 changed files with 71 additions and 48 deletions
+5 -3
View File
@@ -1,7 +1,9 @@
{
"path": "C:/mingw/gitea/python_gui/ROMs",
"esde_src": "C:/mingw/gitea/python_gui/ES-DE",
"roms_src": "C:/mingw/gitea/python_gui/ROMs",
"esde_dst": "C:/mingw/gitea/python_gui/SD/ES-DE",
"roms_dst": "C:/mingw/gitea/python_gui/SD/ROMs",
"selected": [
"atarilynx",
"atarist"
"atarilynx"
]
}
+66 -45
View File
@@ -6,104 +6,125 @@ 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("500x400")
self.root.geometry("600x550")
self.path_var = tk.StringVar()
# 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()
# --- UI ---
path_frame = tk.Frame(root)
path_frame.pack(fill="x", padx=10, pady=10)
# ---------------------------
# RUTAS DE ORIGEN
# ---------------------------
tk.Label(root, text="Rutas de origen", font=("Arial", 12, "bold")).pack(pady=(10, 0))
tk.Label(path_frame, text="Ruta:").pack(side="left")
tk.Entry(path_frame, textvariable=self.path_var, width=40).pack(side="left", padx=5)
tk.Button(path_frame, text="Buscar ruta", command=self.choose_path).pack(side="left")
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)
self.listbox = tk.Listbox(root, selectmode=tk.MULTIPLE)
self.listbox.pack(fill="both", expand=True, padx=10, pady=10)
# ---------------------------
# LISTA DE DIRECTORIOS
# ---------------------------
tk.Label(root, text="Directorios encontrados en ROMs:", font=("Arial", 11)).pack(pady=(15, 5))
tk.Button(root, text="Mostrar selección", command=self.show_selection).pack(pady=5)
self.listbox = tk.Listbox(root, selectmode=tk.MULTIPLE, height=15)
self.listbox.pack(fill="both", expand=True, padx=10)
# --- Eventos ---
# ---------------------------
# 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 ---
# Cargar configuración previa
self.load_config()
# ---------------------------------------------------------
# LÓGICA DE DIRECTORIOS
# CREAR SELECTOR DE RUTA
# ---------------------------------------------------------
def choose_path(self):
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
self.path_var.set(path)
self.update_directory_list(path)
var.set(path)
if callback:
callback(path)
# ---------------------------------------------------------
# LISTA DE DIRECTORIOS
# ---------------------------------------------------------
def update_directory_list(self, path):
self.listbox.delete(0, tk.END)
try:
dirs = [d for d in os.listdir(path) if os.path.isdir(os.path.join(path, d))]
dirs.sort()
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}")
def show_selection(self):
selected = [self.listbox.get(i) for i in self.listbox.curselection()]
messagebox.showinfo("Seleccionados", "\n".join(selected) if selected else "Nada seleccionado")
# ---------------------------------------------------------
# PERSISTENCIA
# ---------------------------------------------------------
def save_config(self):
data = {
"path": self.path_var.get(),
"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()]
}
print("\n[DEBUG] Intentando guardar configuración...")
print("[DEBUG] Ruta del archivo:", os.path.abspath(CONFIG_FILE))
print("[DEBUG] Datos a guardar:", data)
try:
with open(CONFIG_FILE, "w", encoding="utf-8") as f:
json.dump(data, f, indent=4)
print("[DEBUG] Guardado correctamente.")
except Exception as e:
print("[DEBUG] ERROR al guardar:", repr(e))
messagebox.showerror("Error", f"No se pudo guardar la configuración:\n{e}")
def load_config(self):
print("[DEBUG] Buscando archivo de configuración...")
if not os.path.exists(CONFIG_FILE):
print("[DEBUG] No existe config.json, nada que cargar.")
return
print("[DEBUG] config.json encontrado:", os.path.abspath(CONFIG_FILE))
try:
with open(CONFIG_FILE, "r", encoding="utf-8") as f:
data = json.load(f)
print("[DEBUG] Configuración cargada:", data)
except Exception as e:
print("[DEBUG] ERROR al cargar:", repr(e))
except Exception:
return
# Restaurar ruta
path = data.get("path", "")
if path and os.path.isdir(path):
self.path_var.set(path)
self.update_directory_list(path)
# 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", []))