44import os
55import sys
66import time
7+ from importlib import metadata
8+ from utils .runtime_paths import find_resource_root
79from PyQt5 .QtWidgets import (
810 QApplication , QMainWindow , QWidget , QVBoxLayout , QHBoxLayout ,
911 QSplitter , QGroupBox , QLabel , QTextEdit , QPushButton ,
1820PROJECT_ROOT = os .path .dirname (os .path .dirname (os .path .abspath (__file__ )))
1921if 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
2231SUB_DIRS = [
2332 os .path .join (PROJECT_ROOT , 'data_structure' ),
@@ -71,13 +80,25 @@ def _register_qt_metatypes():
7180GLOBAL_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+
7491def _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 ()
0 commit comments