赞
踩
目录
提示QT_DEVICE_PIXEL_RATIO is deprecated
不做详细介绍,只是简单记录,满足一般使用需求,方便查询。
QProcess用于启动外部程序并与之通信。
个人理解:
直接提供一些示例函数
- def process_finished():
- '''
- 进程运行结束的回调函数
- '''
- print(">> Process finished!")
-
-
- process = QProcess()
- process.finished.connect(process_finished)
- process.start("python3", ['dummy_script.py'])
- # 也可 process.start('main.exe')
- process.waitForStarted(2000)
- def kill_process(process):
- process.terminate()
- process.waitForFinished()
- process.kill()
如果无效可以更换为:
- def kill_process(process):
- def kill_finished():
- pass
- cmd = 'taskkill -t -f /pid ' + str(process.processId())
- p = QProcess()
- p.finished.connect(kill_finished)
- p.startDetached(cmd)
- class MyMainWindow(Ui_MainWindow):
- def __init__(self) -> None:
- super().__init__()
- self.current_process = None
-
- def process_finished(self):
- '''
- 进程运行结束的回调函数
- '''
- print(">> Process finished!")
- self.current_process = None
-
- def do_run_program(self, exe_path, gui_name=''):
- '''
- 调用子进程实际运行某个具体的小程序
- exe_path: exe所在相对路径或绝对路径
- gui_name: 显示名称,可空
- '''
- # 确保当前小程序存在
- if not os.path.exists(exe_path):
- print('>> [×]exe文件不存在或错误 => ' + exe_path)
- return False
- # 确保当前只有一个小程序在运行
- if self.current_process is None:
- self.debug('>> 即将运行 => %s[%s]' % (gui_name, exe_path))
- self.current_process = QProcess()
- self.current_process.finished.connect(self.process_finished)
- self.current_process.start(exe_path)
- self.current_process.waitForStarted(2000)
- print('>> [√]启动成功,请稍等几秒')
- return True
- else:
- print('>> [×]已有其他程序正在运行,请先停止')
- return False
- # 消除warning
- def suppress_qt_warnings():
- os.environ["QT_DEVICE_PIXEL_RATIO"] = "0"
- os.environ["QT_AUTO_SCREEN_SCALE_FACTOR"] = "1"
- os.environ["QT_SCREEN_SCALE_FACTORS"] = "1"
- os.environ["QT_SCALE_FACTOR"] = "1"
-
- suppress_qt_warnings()
目前暂未测试,可参考官网或以下教程:Run external programs in PyQt5 with QProcess, with streams and progress bars
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。