简体   繁体   中英

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. 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.

In your current code, your application ends at soon as it starts, because you didn't call 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. This works, given you replace child by self.child :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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