version 1
This commit is contained in:
+110
@@ -0,0 +1,110 @@
|
|||||||
|
import os
|
||||||
|
import json
|
||||||
|
import tkinter as tk
|
||||||
|
from tkinter import filedialog, messagebox
|
||||||
|
|
||||||
|
CONFIG_FILE = "config.json"
|
||||||
|
|
||||||
|
class DirectorySelectorApp:
|
||||||
|
def __init__(self, root):
|
||||||
|
self.root = root
|
||||||
|
self.root.title("Selector de Directorios")
|
||||||
|
self.root.geometry("500x400")
|
||||||
|
|
||||||
|
self.path_var = tk.StringVar()
|
||||||
|
|
||||||
|
# --- UI ---
|
||||||
|
path_frame = tk.Frame(root)
|
||||||
|
path_frame.pack(fill="x", padx=10, pady=10)
|
||||||
|
|
||||||
|
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.listbox = tk.Listbox(root, selectmode=tk.MULTIPLE)
|
||||||
|
self.listbox.pack(fill="both", expand=True, padx=10, pady=10)
|
||||||
|
|
||||||
|
tk.Button(root, text="Mostrar selección", command=self.show_selection).pack(pady=5)
|
||||||
|
|
||||||
|
# --- Eventos ---
|
||||||
|
self.root.protocol("WM_DELETE_WINDOW", self.on_close)
|
||||||
|
|
||||||
|
# --- Cargar configuración previa ---
|
||||||
|
self.load_config()
|
||||||
|
|
||||||
|
# ---------------------------------------------------------
|
||||||
|
# LÓGICA DE DIRECTORIOS
|
||||||
|
# ---------------------------------------------------------
|
||||||
|
def choose_path(self):
|
||||||
|
path = filedialog.askdirectory()
|
||||||
|
if not path:
|
||||||
|
return
|
||||||
|
self.path_var.set(path)
|
||||||
|
self.update_directory_list(path)
|
||||||
|
|
||||||
|
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()
|
||||||
|
|
||||||
|
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(),
|
||||||
|
"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 ruta
|
||||||
|
path = data.get("path", "")
|
||||||
|
if path and os.path.isdir(path):
|
||||||
|
self.path_var.set(path)
|
||||||
|
self.update_directory_list(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()
|
||||||
Reference in New Issue
Block a user