""" SSHClient 入口 """ import sys import os from pathlib import Path # 让 PyInstaller 打包后能找到资源 def _setup_path(): if getattr(sys, "frozen", False): # 打包后:exe 所在目录 bundle_dir = Path(sys.executable).parent sys.path.insert(0, str(bundle_dir)) else: # 源码运行:项目根 sys.path.insert(0, str(Path(__file__).parent)) _setup_path() from PyQt5.QtWidgets import QApplication from PyQt5.QtCore import Qt from PyQt5.QtGui import QFont, QIcon, QPixmap from ui.main_window import MainWindow, APP_NAME def _get_icon_path() -> str: """找到 icon.ico / icon.png 的路径""" # 搜索目录列表:源码运行 / PyInstaller onefile / PyInstaller onedir search_dirs = [] if getattr(sys, "frozen", False): # PyInstaller onefile: 资源解压到 _MEIPASS meipass = getattr(sys, "_MEIPASS", None) if meipass: search_dirs.append(Path(meipass)) search_dirs.append(Path(meipass) / "resources") # exe 同目录(onedir 模式或用户手动放) search_dirs.append(Path(sys.executable).parent) search_dirs.append(Path(sys.executable).parent / "resources") else: search_dirs.append(Path(__file__).parent) search_dirs.append(Path(__file__).parent / "resources") for d in search_dirs: for name in ("icon.png", "icon.ico"): p = d / name if p.exists(): return str(p) return "" def main(): # Windows 上高 DPI 适配 if hasattr(Qt, "AA_EnableHighDpiScaling"): QApplication.setAttribute(Qt.AA_EnableHighDpiScaling, True) if hasattr(Qt, "AA_UseHighDpiPixmaps"): QApplication.setAttribute(Qt.AA_UseHighDpiPixmaps, True) app = QApplication(sys.argv) app.setApplicationName(APP_NAME) app.setStyle("Fusion") # 全局等宽字体偏好 f = QFont("Consolas", 10) app.setFont(f) # 设置应用图标(任务栏 + 窗口标题栏) icon_path = _get_icon_path() if icon_path: app.setWindowIcon(QIcon(icon_path)) w = MainWindow() if icon_path: w.setWindowIcon(QIcon(icon_path)) w.show() sys.exit(app.exec_()) if __name__ == "__main__": main()