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
import os
import sys
import threading
import time
import tkinter as tk
@@ -139,6 +140,18 @@ class MainWindow(ctk.CTk):
# ── 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:
"""Find the user configuration file.
@@ -150,8 +163,7 @@ class MainWindow(ctk.CTk):
Returns:
Path to user config file, or None if not found.
"""
# Navigate from src/gui/main_window.py -> project root
app_dir = Path(__file__).parent.parent.parent
app_dir = self._get_app_dir()
# 1. Check for user config at project root
user_config = app_dir / "config.yaml"
@@ -174,13 +186,12 @@ class MainWindow(ctk.CTk):
def _get_user_config_path(self) -> Path:
"""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:
Path to user config file.
"""
# Navigate from src/gui/main_window.py -> project root
app_dir = Path(__file__).parent.parent.parent
app_dir = self._get_app_dir()
config_path = app_dir / "config.yaml"
config_path.parent.mkdir(parents=True, exist_ok=True)
return config_path