Persistència del TTF i tamany entre obertures de la GUI
This commit is contained in:
@@ -11,3 +11,6 @@ venv/
|
||||
# (p. ej. ejemplos o tests).
|
||||
/*.gif
|
||||
/*.fnt
|
||||
|
||||
# Estado persistente de la GUI (último TTF/tamaño)
|
||||
/.font_gen_gui_state.json
|
||||
|
||||
@@ -8,6 +8,7 @@ a este script al pulsar GENERAR.
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
import tkinter as tk
|
||||
from tkinter import filedialog, messagebox, ttk
|
||||
@@ -18,6 +19,7 @@ import font_gen
|
||||
|
||||
|
||||
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||
STATE_FILE = os.path.join(SCRIPT_DIR, ".font_gen_gui_state.json")
|
||||
PREVIEW_MAX_W = 720
|
||||
PREVIEW_MAX_H = 320
|
||||
PREVIEW_DEBOUNCE_MS = 200
|
||||
@@ -48,10 +50,17 @@ class FontGenApp(tk.Tk):
|
||||
self._adv_visible = False
|
||||
|
||||
self._build_ui()
|
||||
self._load_state() # antes de los traces: el set() inicial no debe disparar preview
|
||||
|
||||
for v in (self.ttf_var, self.size_var, self.columns_var, self.bw_var, self.bh_var):
|
||||
v.trace_add("write", lambda *_: self._schedule_preview())
|
||||
|
||||
# Disparar un preview inicial si _load_state nos dejó un TTF válido.
|
||||
if self.ttf_var.get().strip():
|
||||
self._schedule_preview()
|
||||
|
||||
self.protocol("WM_DELETE_WINDOW", self._on_close)
|
||||
|
||||
# ------------------------------------------------------------------ UI
|
||||
def _build_ui(self) -> None:
|
||||
root = ttk.Frame(self, padding=8)
|
||||
@@ -285,6 +294,38 @@ class FontGenApp(tk.Tk):
|
||||
f"✓ {os.path.basename(gif_path)} y {os.path.basename(fnt_path)} guardados"
|
||||
)
|
||||
|
||||
# ---------------------------------------------------------- Persistencia
|
||||
def _load_state(self) -> None:
|
||||
try:
|
||||
with open(STATE_FILE, "r", encoding="utf-8") as f:
|
||||
state = json.load(f)
|
||||
except (FileNotFoundError, ValueError, OSError):
|
||||
return
|
||||
ttf = state.get("ttf", "")
|
||||
if isinstance(ttf, str) and ttf and os.path.isfile(ttf):
|
||||
self.ttf_var.set(ttf)
|
||||
if not self.output_var.get().strip():
|
||||
self.output_var.set(os.path.splitext(os.path.basename(ttf))[0])
|
||||
size = state.get("size")
|
||||
if isinstance(size, int) and 4 <= size <= 256:
|
||||
self.size_var.set(size)
|
||||
|
||||
def _save_state(self) -> None:
|
||||
try:
|
||||
size = self.size_var.get()
|
||||
except tk.TclError:
|
||||
size = 8
|
||||
state = {"ttf": self.ttf_var.get().strip(), "size": size}
|
||||
try:
|
||||
with open(STATE_FILE, "w", encoding="utf-8") as f:
|
||||
json.dump(state, f, indent=2)
|
||||
except OSError:
|
||||
pass # nice-to-have, no crítico si no se puede escribir.
|
||||
|
||||
def _on_close(self) -> None:
|
||||
self._save_state()
|
||||
self.destroy()
|
||||
|
||||
|
||||
def _parse_optional_positive_int(s: str) -> int | None:
|
||||
s = s.strip()
|
||||
|
||||
Reference in New Issue
Block a user