fix: resolve config.yaml path to exe directory when frozen (PyInstaller)

This commit is contained in:
2026-06-12 16:55:35 +08:00
parent 0d66f65ef2
commit 13ed0d89f8
+16 -5
View File
@@ -7,6 +7,7 @@ built with CustomTkinter.
from __future__ import annotations from __future__ import annotations
import os import os
import sys
import threading import threading
import time import time
import tkinter as tk import tkinter as tk
@@ -139,6 +140,18 @@ class MainWindow(ctk.CTk):
# ── Config ───────────────────────────────────────────────── # ── Config ─────────────────────────────────────────────────
@staticmethod
def _get_app_dir() -> Path:
"""Return the application directory (where config.yaml lives).
When running as a PyInstaller bundle (frozen), uses the
directory of the .exe file. Otherwise navigates from
``__file__`` up to the project root.
"""
if getattr(sys, 'frozen', False):
return Path(sys.executable).parent
return Path(__file__).parent.parent.parent
def _find_config(self) -> Path | None: def _find_config(self) -> Path | None:
"""Find the user configuration file. """Find the user configuration file.
@@ -150,8 +163,7 @@ class MainWindow(ctk.CTk):
Returns: Returns:
Path to user config file, or None if not found. Path to user config file, or None if not found.
""" """
# Navigate from src/gui/main_window.py -> project root app_dir = self._get_app_dir()
app_dir = Path(__file__).parent.parent.parent
# 1. Check for user config at project root # 1. Check for user config at project root
user_config = app_dir / "config.yaml" user_config = app_dir / "config.yaml"
@@ -174,13 +186,12 @@ class MainWindow(ctk.CTk):
def _get_user_config_path(self) -> Path: def _get_user_config_path(self) -> Path:
"""Get the path to the user config file (config.yaml). """Get the path to the user config file (config.yaml).
Creates config.yaml in project root if it doesn't exist. Creates config.yaml in app directory if it doesn't exist.
Returns: Returns:
Path to user config file. Path to user config file.
""" """
# Navigate from src/gui/main_window.py -> project root app_dir = self._get_app_dir()
app_dir = Path(__file__).parent.parent.parent
config_path = app_dir / "config.yaml" config_path = app_dir / "config.yaml"
config_path.parent.mkdir(parents=True, exist_ok=True) config_path.parent.mkdir(parents=True, exist_ok=True)
return config_path return config_path