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