Skip to content

Commit e3e2dd1

Browse files
committed
feat(gui): 改进版本和资源文件加载逻辑
添加对资源文件的动态查找功能,支持从多个位置加载VERSION文件和图标 使用importlib.metadata作为版本信息备选方案 统一窗口图标设置逻辑
1 parent eae7690 commit e3e2dd1

2 files changed

Lines changed: 31 additions & 14 deletions

File tree

gui/gui_main.py

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import os
55
import sys
66
import time
7+
from importlib import metadata
8+
from utils.runtime_paths import find_resource_root
79
from PyQt5.QtWidgets import (
810
QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout,
911
QSplitter, QGroupBox, QLabel, QTextEdit, QPushButton,
@@ -18,6 +20,13 @@
1820
PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
1921
if PROJECT_ROOT not in sys.path:
2022
sys.path.insert(0, PROJECT_ROOT)
23+
RESOURCE_ROOT = str(
24+
find_resource_root(
25+
__file__,
26+
levels_up=1,
27+
required_paths=("docs",),
28+
)
29+
)
2130

2231
SUB_DIRS = [
2332
os.path.join(PROJECT_ROOT, 'data_structure'),
@@ -71,13 +80,25 @@ def _register_qt_metatypes():
7180
GLOBAL_MESH_DIMENSION = 2
7281

7382

83+
def _resolve_resource_file(*relative_parts):
84+
for base in (RESOURCE_ROOT, PROJECT_ROOT):
85+
candidate = os.path.join(base, *relative_parts)
86+
if os.path.exists(candidate):
87+
return candidate
88+
return None
89+
90+
7491
def _load_app_version():
75-
version_file = os.path.join(PROJECT_ROOT, "VERSION")
76-
if os.path.exists(version_file):
92+
version_file = _resolve_resource_file("VERSION")
93+
if version_file:
7794
with open(version_file, "r", encoding="utf-8") as f:
7895
version = f.read().strip()
7996
if version:
8097
return version
98+
try:
99+
return metadata.version("PyMeshGen")
100+
except metadata.PackageNotFoundError:
101+
pass
81102
return "0.0.0"
82103

83104

@@ -104,19 +125,11 @@ def _setup_window(self):
104125
f"PyMeshGen V{_load_app_version()} - 基于Python的网格生成工具"
105126
)
106127

107-
# Use only the docs/icon.png file as requested
108-
icon_path = os.path.join(PROJECT_ROOT, "docs", "icon.png")
109-
110-
# Try to set the icon from the docs directory
128+
# 优先使用 docs/icon.png,若不存在则尝试 docs/icon.ico
111129
try:
112-
from PyQt5.QtGui import QIcon
113-
if os.path.exists(icon_path):
130+
icon_path = _resolve_resource_file("docs", "icon.png") or _resolve_resource_file("docs", "icon.ico")
131+
if icon_path:
114132
self.setWindowIcon(QIcon(icon_path))
115-
else:
116-
# If the icon file doesn't exist, try alternative path
117-
alt_icon_path = os.path.join(PROJECT_ROOT, "..", "docs", "icon.png")
118-
if os.path.exists(alt_icon_path):
119-
self.setWindowIcon(QIcon(alt_icon_path))
120133
except Exception as e:
121134
# If icon setting fails, log the error but continue
122135
print(f"Could not set application icon: {e}")
@@ -2342,7 +2355,10 @@ def main():
23422355
"""主函数"""
23432356
app = QApplication(sys.argv)
23442357
app.setApplicationName("PyMeshGen")
2345-
app.setApplicationVersion("1.0")
2358+
app.setApplicationVersion(_load_app_version())
2359+
icon_path = _resolve_resource_file("docs", "icon.png") or _resolve_resource_file("docs", "icon.ico")
2360+
if icon_path:
2361+
app.setWindowIcon(QIcon(icon_path))
23462362

23472363
window = PyMeshGenGUI()
23482364
window.show()

packaging/windows/PyMeshGen.spec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ datas = [
7777
# 配置文件目录
7878
(os.path.join(project_root, 'config'), 'config'),
7979
(os.path.join(project_root, 'config', 'input'), 'config\\input'),
80+
(os.path.join(project_root, 'VERSION'), '.'),
8081

8182
# 文档和图标
8283
(os.path.join(project_root, 'docs'), 'docs'),

0 commit comments

Comments
 (0)