import os import sys import time # sys.path.append(os.path.dirname(__file__)) # 必须要把 ui 目录添加到系统路径,否则子目录文件 ui_MainWindows.py 无法 import 资源文件 sys.path.append(os.path.join(os.path.dirname(__file__), 'ui')) import threading from PySide2.QtWidgets import QApplication from PySide2.QtCore import QObject from PySide2 import QtCore from main_windows import MainWindow QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling) QApplication.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps, True) app = None mainWindows:MainWindow = None sub_thread = None exit_flag = False def run_forever(GUI): global app,mainWindows QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_ShareOpenGLContexts) app = QApplication(sys.argv) mainWindows = MainWindow(GUI) mainWindows.sig.connect(handle_signal) mainWindows.show_analyze_window() app.exec_() def run_in_thread(GUI:QObject): global sub_thread sub_thread = threading.Thread(target=run_forever, args=(GUI,)) sub_thread.setDaemon(True) sub_thread.start() # 需等待线程启动成功,否则会丢失 _GUI 打印的版本信息 time.sleep(0.05) return sub_thread def signale(dat): mainWindows.sig.emit(dat) def handle_signal(dat): if isinstance(dat, str): dat = dat.lower() if dat == 'start': mainWindows.show() elif dat == 'stop': mainWindows.hide() elif dat == 'exit': mainWindows.nc_shell_thread.quit() mainWindows.nc_shell_thread.wait(5) mainWindows.close() else: mainWindows.sub_win.sig.emit(dat) def StopGui(): return mainWindows.hide() def StartGui(): return mainWindows.show() def exit(): signale('exit') sub_thread.join() if __name__ == '__main__': run_forever(1)