修复了exe路径问题

This commit is contained in:
2025-08-18 01:57:03 +08:00
parent c90d0b4d79
commit 898c5dfb2b
5 changed files with 43 additions and 30 deletions

View File

@@ -78,4 +78,33 @@ class CodeGenerator:
if not os.path.exists(template_dir):
print(f"警告:模板目录不存在: {template_dir}")
return template_dir
return template_dir
@staticmethod
def get_assets_dir(sub_path=""):
"""获取assets目录路径兼容打包环境
Args:
sub_path: 子路径,如 "User_code/component""User_code/device"
Returns:
str: 完整的assets路径
"""
if getattr(sys, 'frozen', False):
# 打包后的环境
base_path = sys._MEIPASS
assets_dir = os.path.join(base_path, "assets")
else:
# 开发环境
current_dir = os.path.dirname(os.path.abspath(__file__))
while os.path.basename(current_dir) != 'MRobot' and current_dir != '/':
current_dir = os.path.dirname(current_dir)
assets_dir = os.path.join(current_dir, "assets")
if sub_path:
full_path = os.path.join(assets_dir, sub_path)
else:
full_path = assets_dir
if not os.path.exists(full_path):
print(f"警告:资源目录不存在: {full_path}")
return full_path