繁体   English   中英

PyQt错误“QProcess:进程仍在运行时被销毁”

[英]PyQt error “QProcess: Destroyed while process is still running”

当我尝试运行以下PyQt代码来运行进程和tmux时,我遇到错误QProcess: Destroyed while process is still running. 我怎样才能解决这个问题?

#!/usr/bin/env python
#-*- coding:utf-8 -*-

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class embeddedTerminal(QWidget):

    def __init__(self):
        QWidget.__init__(self)
        self._processes = []
        self.resize(800, 600)
        self.terminal = QWidget(self)
        layout = QVBoxLayout(self)
        layout.addWidget(self.terminal)
        self._start_process(
            'xterm',
            ['-into', str(self.terminal.winId()),
             '-e', 'tmux', 'new', '-s', 'my_session']
        )
        button = QPushButton('list files')
        layout.addWidget(button)
        button.clicked.connect(self._list_files)

    def _start_process(self, prog, args):
        child = QProcess()
        self._processes.append(child)
        child.start(prog, args)

    def _list_files(self):
        self._start_process(
            'tmux', ['send-keys', '-t', 'my_session:0', 'ls', 'Enter']
        )

if __name__ == "__main__":
    app = QApplication(sys.argv)
    main = embeddedTerminal()
    main.show()

当应用程序关闭且进程尚未完成时QProcess: Destroyed while process is still running时,通常会收到错误QProcess: Destroyed while process is still running

在您当前的代码中,您的应用程序会在启动时立即结束,因为您没有调用app.exec_() 你应该做的事情如下:

if __name__ == "__main__":
    app = QApplication(sys.argv)
    main = embeddedTerminal()
    main.show()
    sys.exit(app.exec_())

现在,它工作正常,但当您关闭应用程序时,您仍将收到错误消息。 您需要覆盖close事件才能正确结束该过程。 这是有效的,给你替换self.child child

def closeEvent(self,event):
    self.child.terminate()
    self.child.waitForFinished()
    event.accept()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM