From 939473716c0eab12d55ca6b47514004c3af33f67 Mon Sep 17 00:00:00 2001 From: Eric-Terminal <121368508+Eric-Terminal@users.noreply.github.com> Date: Fri, 5 Sep 2025 00:42:34 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=89=93=E5=8C=85=E4=BB=A5?= =?UTF-8?q?=E5=90=8E=E6=97=A0=E6=B3=95=E8=AF=BB=E5=8F=96=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build-windows.yml | 3 +-- main.py | 27 ++++++++++++++------------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/.github/workflows/build-windows.yml b/.github/workflows/build-windows.yml index 5dc65aa..54a8da4 100644 --- a/.github/workflows/build-windows.yml +++ b/.github/workflows/build-windows.yml @@ -41,11 +41,10 @@ jobs: # 第4步:使用PyInstaller打包 # --noconsole: 这是一个GUI应用,不显示控制台窗口 # --onefile: 打包成单个.exe文件 - # --add-data: 将 config.json 文件包含到exe中 # --name: 指定生成的可执行文件名 - name: Build with PyInstaller shell: cmd - run: pyinstaller --noconsole --onefile --name "AI-Essay-Corrector" --add-data "config.json;." main.py + run: pyinstaller --noconsole --onefile --name "AI-Essay-Corrector" main.py # 第5步:将打包好的.exe上传,以便在工作流页面下载 (适合测试) - name: Upload artifact for testing diff --git a/main.py b/main.py index eaf41e9..d8c8475 100644 --- a/main.py +++ b/main.py @@ -5,26 +5,27 @@ from api_services import ApiService import sys import os -def resource_path(relative_path): - """ - 获取资源的绝对路径,以支持PyInstaller打包后的单文件应用。 - 在开发环境中,返回基于当前工作目录的相对路径。 - 在PyInstaller打包的应用中,返回临时文件夹`_MEIPASS`中的路径。 +def get_config_path(): + """ + 获取配置文件的合适路径。 + 在开发环境使用当前目录,在打包环境使用exe所在目录。 """ try: - # 尝试获取PyInstaller在运行时创建的临时路径 - base_path = sys._MEIPASS + # 检查是否在PyInstaller打包环境中 + sys._MEIPASS + # 如果是打包环境,使用exe文件所在目录 + exe_dir = os.path.dirname(sys.executable) + return os.path.join(exe_dir, "config.json") except Exception: - # 如果`_MEIPASS`属性不存在,说明是在开发环境,使用当前目录 - base_path = os.path.abspath(".") - - return os.path.join(base_path, relative_path) + # 开发环境,使用当前目录 + return "config.json" if __name__ == "__main__": # 1. 初始化核心服务 - # 使用 resource_path 确保在打包后也能正确找到配置文件 - config_manager = ConfigManager(resource_path("config.json")) + # 使用合适的配置文件路径 + config_path = get_config_path() + config_manager = ConfigManager(config_path) # 2. 创建Tkinter主窗口 root = tk.Tk()