简体   繁体   English

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

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

When I try to run the following PyQt code for running processes and tmux, I encounter the error QProcess: Destroyed while process is still running. 当我尝试运行以下PyQt代码来运行进程和tmux时,我遇到错误QProcess: Destroyed while process is still running. How can I fix this? 我怎样才能解决这个问题?

#!/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()

You usually get the error QProcess: Destroyed while process is still running when the application closes and the process hadn't finished. 当应用程序关闭且进程尚未完成时QProcess: Destroyed while process is still running时,通常会收到错误QProcess: Destroyed while process is still running

In your current code, your application ends at soon as it starts, because you didn't call app.exec_() . 在您当前的代码中,您的应用程序会在启动时立即结束,因为您没有调用app.exec_() You should do something like: 你应该做的事情如下:

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

Now, it works fine, but when you close the application you will still get the error message. 现在,它工作正常,但当您关闭应用程序时,您仍将收到错误消息。 You need to overwrite the close event to end the process properly. 您需要覆盖close事件才能正确结束该过程。 This works, given you replace child by self.child : 这是有效的,给你替换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