python - PyQt错误 “QProcess: Destroyed while process is still running”

标签 python pyqt

当我尝试运行以下 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

在您当前的代码中,您的应用程序一启动就结束,因为您没有调用 app.exec_()。你应该这样做:

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

现在,它工作正常,但是当您关闭应用程序时,您仍然会收到错误消息。您需要覆盖关闭事件才能正确结束该过程。如果您将 child 替换为 self.child:

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

关于python - PyQt错误 “QProcess: Destroyed while process is still running”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31480210/

相关文章:

python - PyQt5:DLL加载失败:找不到指定的模块

java - 获取无法编译内联过滤器: <Jsonpath with Query>

python - Zlib 无法解压 python 2.7

python - 将 PyQt Gui 应用程序与 Django 项目相结合

python - 让 Enter 键的行为类似于 QTableWidget 中的 Tab

python - 如何在不触发事件的情况下禁用 QCheckBox?

python - 访问数组特定元素中的邻居索引 (python)

javascript - 如何在 ipython 或 jupyter notebook 中加载外部静态 Javascript 文件

python - 如何将 OAuth2Decorator 与 Google Cloud Storage 结合使用?

python - PyQt/PySide 中是否有默认图标?